Phantom Type Parameters
Unused type parameters can be marked as phantom type parameters, which do not participate in the ability derivation for structs. In this way, arguments to phantom type parameters are not considered when deriving the abilities for generic types, thus avoiding the need for spurious ability annotations. For this relaxed rule to be sound, Move's type system guarantees that a parameter declared as phantom
is either not used at all in the struct definition, or it is only used as an argument to type parameters also declared as phantom
.
Declaration
In a struct definition a type parameter can be declared as phantom by adding the phantom
keyword before its declaration. If a type parameter is declared as phantom we say it is a phantom type parameter. When defining a struct, Move's type checker ensures that every phantom type parameter is either not used inside the struct definition or it is only used as an argument to a phantom type parameter.
More formally, if a type is used as an argument to a phantom type parameter we say the type appears in phantom position. With this definition in place, the rule for the correct use of phantom parameters can be specified as follows: A phantom type parameter can only appear in phantom position.
The following two examples show valid uses of phantom parameters. In the first one, the parameter T1
is not used at all inside the struct definition. In the second one, the parameter T1
is only used as an argument to a phantom type parameter.
The following code shows examples of violations of the rule:
Instantiation
When instantiating a struct, the arguments to phantom parameters are excluded when deriving the struct abilities. For example, consider the following code:
Consider now the type S<HasCopy, NoCopy>
. Since S
is defined with copy
and all non-phantom arguments have copy
then S<HasCopy, NoCopy>
also has copy
.
Phantom Type Parameters with Ability Constraints
Ability constraints and phantom type parameters are orthogonal features in the sense that phantom parameters can be declared with ability constraints. When instantiating a phantom type parameter with an ability constraint, the type argument has to satisfy that constraint, even though the parameter is phantom. For example, the following definition is perfectly valid:
The usual restrictions apply and T
can only be instantiated with arguments having copy
.
Last updated