constraints.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. // Signed is a constraint that permits any signed integer type.
  8. // If future releases of Go add new predeclared signed integer types,
  9. // this constraint will be modified to include them.
  10. type Signed interface {
  11. ~int | ~int8 | ~int16 | ~int32 | ~int64
  12. }
  13. // Unsigned is a constraint that permits any unsigned integer type.
  14. // If future releases of Go add new predeclared unsigned integer types,
  15. // this constraint will be modified to include them.
  16. type Unsigned interface {
  17. ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
  18. }
  19. // Integer is a constraint that permits any integer type.
  20. // If future releases of Go add new predeclared integer types,
  21. // this constraint will be modified to include them.
  22. type Integer interface {
  23. Signed | Unsigned
  24. }
  25. // Float is a constraint that permits any floating-point type.
  26. // If future releases of Go add new predeclared floating-point types,
  27. // this constraint will be modified to include them.
  28. type Float interface {
  29. ~float32 | ~float64
  30. }
  31. // Complex is a constraint that permits any complex numeric type.
  32. // If future releases of Go add new predeclared complex numeric types,
  33. // this constraint will be modified to include them.
  34. type Complex interface {
  35. ~complex64 | ~complex128
  36. }
  37. // Ordered is a constraint that permits any ordered type: any type
  38. // that supports the operators < <= >= >.
  39. // If future releases of Go add new ordered types,
  40. // this constraint will be modified to include them.
  41. type Ordered interface {
  42. Integer | Float | ~string
  43. }