Reified Generics: Reified Generics Migration
Migration Features for Reified Generics
In order to make migrating to reified generics easier, we have added the following migration features:
- The
<<__Soft>>
annotation on a type parameter implies the intent to switch it to reified state. If generics at call sites are not provided the type checker will raise an error, and the runtime will raise a warning so you can capture these locations through logging. The generic cannot be used in the body of the function. - The
<<__Warn>>
annotation on a type parameter implies that if the reified generic is incorrect at parameter or return type hints locations, the runtime will raise a warning instead of type hint violation. - The
get_type_structure<reify T>()
function, given a reified type, returns the type structure representing the type. - The
get_classname<reify T>()
function, given a reified type, returns the name of the class represented by this type, or raises an exception if the type does not represent a class.
Example Incremental Migration
In this part, we'll walk you through how to migrate a non reified function to a reified function. Some of these steps can be skipped depending on the use case.
- Beginning of time
class C<T> {}
function f(C<int> $x): void {}
function demo(): void {
f(new C()); // OK
}
- You have managed to write out all the type annotations (either pre-existing or by using
<<__Soft>>
annotation logging)
class C<<<__Soft>> reify T> {}
function f(C<int> $x): void {}
function demo(): void {
f(new C<string>()); // Typechecker error: string incompatible with int
}
- You now want to remove
__Soft
and start using the generic. So you move__Soft
to__Warn
.
class C<<<__Warn>> reify T> {}
function f(C<int> $x): void {}
function demo(): void {
f(new C<string>()); // Runtime warning: string incompatible with int
}
- By using logging, you have added
__Soft
to everywhere it's used and now it will be safe to remove__Warn
.
class C<<<__Warn>> reify T> {}
function f(C< <<__Soft>> int> $x): void {}
function demo(): void {
f(new C<string>()); // Runtime warning: string incompatible with int
}
__Warn
goes away
class C<reify T> {}
function f(C< <<__Soft>> int> $x): void {}
function demo(): void {
f(new C<string>()); // Runtime warning: string incompatible with int
}
- Fix the use site
class C<reify T> {}
function f(C<string> $x): void {}
function demo(): void {
f(new C<string>()); // OK
}
Thank You!
Thank You! If you'd like to share more feedback, please file an issue.