constraints.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2021 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package constraints defines a set of useful constraints to be used
  5. // with type parameters.
  6. package constraints
  7. import "cmp"
  8. // Signed is a constraint that permits any signed integer type.
  9. // If future releases of Go add new predeclared signed integer types,
  10. // this constraint will be modified to include them.
  11. type Signed interface {
  12. ~int | ~int8 | ~int16 | ~int32 | ~int64
  13. }
  14. // Unsigned is a constraint that permits any unsigned integer type.
  15. // If future releases of Go add new predeclared unsigned integer types,
  16. // this constraint will be modified to include them.
  17. type Unsigned interface {
  18. ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
  19. }
  20. // Integer is a constraint that permits any integer type.
  21. // If future releases of Go add new predeclared integer types,
  22. // this constraint will be modified to include them.
  23. type Integer interface {
  24. Signed | Unsigned
  25. }
  26. // Float is a constraint that permits any floating-point type.
  27. // If future releases of Go add new predeclared floating-point types,
  28. // this constraint will be modified to include them.
  29. type Float interface {
  30. ~float32 | ~float64
  31. }
  32. // Complex is a constraint that permits any complex numeric type.
  33. // If future releases of Go add new predeclared complex numeric types,
  34. // this constraint will be modified to include them.
  35. type Complex interface {
  36. ~complex64 | ~complex128
  37. }
  38. // Ordered is a constraint that permits any ordered type: any type
  39. // that supports the operators < <= >= >.
  40. // If future releases of Go add new ordered types,
  41. // this constraint will be modified to include them.
  42. //
  43. // This type is redundant since Go 1.21 introduced [cmp.Ordered].
  44. //
  45. //go:fix inline
  46. type Ordered = cmp.Ordered