@@ -9,9 +9,9 @@ import "math"
99// Numeric is a contract that matches any numeric type.
1010// It would likely be in a contracts package in the standard library.
1111type Numeric interface {
12- type int , int8 , int16 , int32 , int64 , uint , uint8 ,
13- uint16 , uint32 , uint64 , uintptr , float32 ,
14- float64 , complex64 , complex128
12+ ~ int | ~ int8 | ~ int16 | ~ int32 | ~ int64 | ~ uint | ~ uint8 |
13+ ~ uint16 | ~ uint32 | ~ uint64 | ~ uintptr | ~ float32 |
14+ ~ float64 | ~ complex64 | ~ complex128
1515}
1616
1717// NumericAbs matches numeric types with an Abs method.
@@ -22,13 +22,13 @@ type NumericAbs[T any] interface {
2222
2323// OrderedNumeric matches numeric types that support the < operator.
2424type OrderedNumeric interface {
25- type int , int8 , int16 , int32 , int64 , uint , uint8 ,
26- uint16 , uint32 , uint64 , uintptr , float32 , float64
25+ ~ int | ~ int8 | ~ int16 | ~ int32 | ~ int64 | ~ uint | ~ uint8 |
26+ ~ uint16 | ~ uint32 | ~ uint64 | ~ uintptr | ~ float32 | ~ float64
2727}
2828
2929// Complex matches the two complex types, which do not have a < operator.
3030type Complex interface {
31- type complex64 , complex128
31+ ~ complex64 | ~ complex128
3232}
3333
3434func DotProduct [T Numeric ](s1 , s2 []T ) T {
@@ -44,7 +44,7 @@ func DotProduct[T Numeric](s1, s2 []T) T {
4444
4545// AbsDifference computes the absolute value of the difference of
4646// a and b, where the absolute value is determined by the Abs method.
47- func AbsDifference [T NumericAbs ](a , b T ) T {
47+ func AbsDifference [T NumericAbs [ T ] ](a , b T ) T {
4848 d := a - b
4949 return d .Abs ()
5050}
0 commit comments