Skip to content

Commit

Permalink
Add Compare method which forwards calls to Cmp (#346)
Browse files Browse the repository at this point in the history
Given the interface definition

	type Ordered[T any] interface {
		Compare(T) int
	}

And the type constraint T Ordered[T], make decimal.Decimal satisfy this
constraint, so that generic code written against T Ordered[T] can work
with decimal values as smoothly as it works with time.Time values today.

Fixes: #345
  • Loading branch information
acln0 committed Jan 24, 2024
1 parent b79c571 commit 57a340d
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions decimal.go
Expand Up @@ -981,6 +981,15 @@ func (d Decimal) Cmp(d2 Decimal) int {
return rd.value.Cmp(rd2.value)
}

// Compare compares the numbers represented by d and d2 and returns:
//
// -1 if d < d2
// 0 if d == d2
// +1 if d > d2
func (d Decimal) Compare(d2 Decimal) int {
return d.Cmp(d2)
}

// Equal returns whether the numbers represented by d and d2 are equal.
func (d Decimal) Equal(d2 Decimal) bool {
return d.Cmp(d2) == 0
Expand Down

0 comments on commit 57a340d

Please sign in to comment.