Go Reference ยท Built-in Types
Go constraints and the ~T approximation element
Reference for Go constraint interfaces: type sets, union elements, the ~T approximation operator, cmp.Ordered, embedding and core-type rules.
A constraint is an interface that describes the set of types a type parameter may take. Beyond method lists, a constraint interface can contain type elements: a single type (int), a union (int | float64), or an approximation ~int, which means "int and every type whose underlying type is int".
The set of types an interface allows is its type set. Inside a generic function you may use exactly the operations that all types in the type set support.
type Number interface { ~int | ~int64 | ~float64 }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
interface{ int | string } // union of exact types interface{ ~int } // underlying type int interface{ ~[]E } // used with a second type parameter E interface{ comparable; Name() string }
| Element | Meaning |
|---|---|
T |
Exactly the type T. |
~T |
All types whose underlying type is T. T itself must be its own underlying type (a non-defined type). |
A | B |
Union of the type sets. |
| Several lines | Intersection: a type must satisfy all of them. |
| Methods | Listed alongside type elements; types must have those methods. |
Errors: an interface with type elements can only be used as a constraint, not as a variable type (var x Number fails). The ~ form cannot be applied to interface types or to a type whose underlying type differs from the operand.
Usage notes
- Why
~? Without it,type Celsius float64would not satisfyfloat64. Use~for numeric constraints so named types are accepted. - Operations come from the whole type set.
+works if every type in the set supports it;<only if all are ordered.cmp.Ordered(Go 1.21) packages all ordered types: integers, floats and strings. - Core types. Operations like
range, indexing andmakeneed the type set to share a single underlying (core) type, which is whyS ~[]Eis the typical way to accept any slice type. - Unions of interfaces with methods are not allowed; a union can contain only non-interface types or interfaces without methods.
- Do not constrain too tightly. A constraint listing
int | float64rejectsint32; the standardgolang.org/x/exp/constraintspackage (now mostly replaced bycmp.Ordered) offersInteger,FloatandSignedfamilies. - Inline constraints are allowed:
[T int | string]instead of naming an interface. - Compile-time only. Type sets are erased from run-time behavior; they affect what compiles, not what executes.
Related
- Type parameters, comparable, any, Generics rules
- Lessons: Constraints, Generics, Generic types