Generics
Generics can be used to define functions and structs over different input data types. This language feature is sometimes referred to as parametric polymorphism. In Move, we will often use the term generics interchangeably with type parameters and type arguments.
Generics are commonly used in library code, such as in Vector, to declare code that works over any possible instantiation (that satisfies the specified constraints). In other frameworks, generic code can sometimes be used to interact with global storage many different ways that all still share the same implementation.
#
Declaring Type ParametersBoth functions and structs can take a list of type parameters in their signatures, enclosed by a pair of angle brackets <...>
.
#
Generic FunctionsType parameters for functions are placed after the function name and before the (value) parameter list. The following code defines a generic identity function that takes a value of any type and returns that value unchanged.
Once defined, the type parameter T
can be used in parameter types, return types, and inside the function body.
#
Generic StructsType parameters for structs are placed after the struct name, and can be used to name the types of the fields.
Note that type parameters do not have to be used
#
Type Arguments#
Calling Generic FunctionsWhen calling a generic function, one can specify the type arguments for the function's type parameters in a list enclosed by a pair of angle brackets.
If you do not specify the type arguments, Move's type inference will supply them for you.
#
Using Generic StructsSimilarly, one can attach a list of type arguments for the struct's type parameters when constructing or destructing values of generic types.
If you do not specify the type arguments, Move's type inference will supply them for you.
#
Type Argument MismatchIf you specify the type arguments and they conflict with the actual values supplied, an error will be given
and similarly
#
Type InferenceIn most cases, the Move compiler will be able to infer the type arguments so you don't have to write them down explicitly. Here's what the examples above would look like if we omit the type arguments.
Note: when the compiler is unable to infer the types, you'll need annotate them manually. A common scenario is to call a function with type parameters appearing only at return positions.
However, the compiler will be able to infer the type if that return value is used later in that function
#
Unused Type ParametersMove allows unused type parameters so the following struct definition is valid:
This can be convenient when modeling certain concepts. Here is an example:
#
ConstraintsIn the examples above, we have demonstrated how one can use type parameters to define "unkonwn" types that can be plugged in by callers at a later time. This however means the type system has little information about the type and has to perform checks in a very conservative way. In some sense, the type system must assume the worst case scenario for an unconstrained generic. If for instance you were to copy
an unconstrained generic, you could break resource safety if that type was instantiated with a resource!
This is where constraints come into play: they offer a way to specify what properties these unknown types have so the type system can allow operations that would otherwise be unsafe.
#
Declaring ConstraintsConstraints can be imposed on type parameters using the following syntax.
resource
means values of the type cannot be copied and cannot be droppedcopyable
means values of the type can be copied and can be dropped
These two constraint are mutually exclusive so you can't have both applied to a type parameter at the same time.
#
Verifying ConstraintsConstraints are checked at call sites so the following code won't compile.
#
How to tell if a struct type is resource or copyableRecall that a non-generic struct type is considered resource if and only if it is explicitly marked so.
However for a generic struct type, whether it is considered resource depends on the specific type arguments used to instantiate it, unless there's an explicit resource
marker in the struct definition.
#
Limitations on Recursions#
Recursive StructsGeneric structs can not contain fields of the same type, either directly or indirectly, even with different type arguments. All of the following struct definitions are invalid:
#
Advanced Topic: Type-level RecursionsMove allows generic functions to be called recursively. However, when used in combination with generic structs, this could create an infinite number of types in certain cases, and allowing this means adding unnecessary complexity to the compiler, vm and other language components. Therefore, such recursions are forbidden.
Allowed:
Not allowed:
Note, the check for type level recursions is based on a conservative analysis on the call sites and does NOT take control flow or runtime values into account.
The function in the example above will technically terminate for any given input and therefore only creating finitely many types, but it is still considered invalid by Move's type system.