constraints.go raw

   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  
   5  // Package constraints defines a set of useful constraints to be used
   6  // with type parameters.
   7  package constraints
   8  
   9  import "cmp"
  10  
  11  // Signed is a constraint that permits any signed integer type.
  12  // If future releases of Go add new predeclared signed integer types,
  13  // this constraint will be modified to include them.
  14  type Signed interface {
  15  	~int | ~int8 | ~int16 | ~int32 | ~int64
  16  }
  17  
  18  // Unsigned is a constraint that permits any unsigned integer type.
  19  // If future releases of Go add new predeclared unsigned integer types,
  20  // this constraint will be modified to include them.
  21  type Unsigned interface {
  22  	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
  23  }
  24  
  25  // Integer is a constraint that permits any integer type.
  26  // If future releases of Go add new predeclared integer types,
  27  // this constraint will be modified to include them.
  28  type Integer interface {
  29  	Signed | Unsigned
  30  }
  31  
  32  // Float is a constraint that permits any floating-point type.
  33  // If future releases of Go add new predeclared floating-point types,
  34  // this constraint will be modified to include them.
  35  type Float interface {
  36  	~float32 | ~float64
  37  }
  38  
  39  // Complex is a constraint that permits any complex numeric type.
  40  // If future releases of Go add new predeclared complex numeric types,
  41  // this constraint will be modified to include them.
  42  type Complex interface {
  43  	~complex64 | ~complex128
  44  }
  45  
  46  // Ordered is a constraint that permits any ordered type: any type
  47  // that supports the operators < <= >= >.
  48  // If future releases of Go add new ordered types,
  49  // this constraint will be modified to include them.
  50  //
  51  // This type is redundant since Go 1.21 introduced [cmp.Ordered].
  52  //
  53  //go:fix inline
  54  type Ordered = cmp.Ordered
  55