Add Compare method which forwards calls to Cmp (#346)

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
This commit is contained in:
Andrei Tudor Călin 2024-01-25 00:48:15 +02:00 committed by GitHub
parent b79c571f80
commit 57a340d853
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -981,6 +981,15 @@ func (d Decimal) Cmp(d2 Decimal) int {
return rd.value.Cmp(rd2.value) 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. // Equal returns whether the numbers represented by d and d2 are equal.
func (d Decimal) Equal(d2 Decimal) bool { func (d Decimal) Equal(d2 Decimal) bool {
return d.Cmp(d2) == 0 return d.Cmp(d2) == 0