Reflect.ErasedType
type Reflect.ErasedTypeReflect.ErasedType is a type value whose static type is not known: what Reflect.erase returns, and what Reflect.shape takes apart. Code that walks a type recurses on erased types, since the element type of an Array[E] is not known statically one level down.
Details
- Three things make a
Reflect.ErasedType:Reflect.eraseof a type value,Reflect.typeof a boxed value, and the children of a shape thatReflect.shapereturns. Reflect.shape,Reflect.assignable, and theReflect.make*builders take an erased type.- Two are
==when they denote the same type, whichever of these made them, and they hash the same, so an erased type works as a map key.String.fromrenders the type in annotation syntax. - No function turns it back into a
Reflect.Type[T]. The static type comes back only at the end, whenReflect.tounboxes a value with a typedtype(T). - It has no
isdiscriminator: a union holds at most one ofReflect.ErasedType,Reflect.Type[T],Reflect.Decl, andReflect.Value, besidenull. Json.stringifypanics on it.
Basic examples
Erase a type value, and the result renders as the type:
let t = Reflect.erase(type(Map[string, Array[int]]))
t // Map[string, Array[int]]Take it apart with Reflect.shape. The children are erased types too:
let t = Reflect.erase(type(Map[string, Array[int]]))
Reflect.shape(t) // Reflect.Map{ key: string, value: Array[int] }Scope
Reflect.type gives the erased type a value was boxed at. A union is resolved to the value's member:
type Celsius = float64
Reflect.type(Reflect.valueOf(type(int | null), 42)) // int
Reflect.type(Reflect.valueOf(type(Celsius), Celsius(21.5))) // Celsius
Reflect.type(Reflect.valueOf(type(Array[string]), ["a", "b"])) // Array[string]Every child of a shape is erased: a set's member, a union's members, a function's parameters and result, and likewise an array's element, a map's key and value, and a record's fields and type arguments:
Reflect.shape(Reflect.erase(type(Set[string]))) // Reflect.Set{ member: string }
Reflect.shape(Reflect.erase(type(int | string | null))) // Reflect.Union{ members: [int, string, null] }
Reflect.shape(Reflect.erase(type(func(int, string): bool))) // Reflect.Func{ params: [int, string], result: bool }An array or a map holds erased types of different static types side by side, which Reflect.Type values cannot do:
let ts: Array[Reflect.ErasedType] = [Reflect.erase(type(int)), Reflect.erase(type(Array[string]))]
ts // [int, Array[string]]Applications
Choose a form widget for each field of a record from a table keyed by erased types, with a fallback for types the table does not list:
type Signup = { email: string, age: int, newsletter: bool, birthday: Time.PlainDate | null }
let widgets: Map[Reflect.ErasedType, string] = Map{
Reflect.erase(type(int)): "number",
Reflect.erase(type(bool)): "checkbox",
Reflect.erase(type(Time.PlainDate | null)): "date",
}
let s = Reflect.shape(Reflect.erase(type(Signup)))
if s is Reflect.Record {
for f in s.fields {
println("${f.name}: ${widgets.get(f.type) ?? "text"}")
}
}email: text
age: number
newsletter: checkbox
birthday: dateList every type a recursive type reaches. A shape describes one level, so a walk that records the erased types it has seen visits each one once and ends:
type Tree = { label: string, children: Array[Tree], parent: Tree | null }
func children(t: Reflect.ErasedType): Array[Reflect.ErasedType] {
let s = Reflect.shape(t)
if s is Reflect.Record {
s.fields.map({ x.type })
} else if s is Reflect.Array {
[s.element]
} else if s is Reflect.Union {
s.members
} else {
[]
}
}
func visit(t: Reflect.ErasedType, seen: Set[Reflect.ErasedType]): void {
if !seen.add(t) {
return
}
for c in children(t) {
visit(c, seen)
}
}
let seen: Set[Reflect.ErasedType] = Set.of([])
visit(Reflect.erase(type(Tree)), seen)
println(seen)Set.of([Tree, string, Array[Tree], Tree | null, null])Properties and relations
An erased type from Reflect.erase, from Reflect.type, or from a shape is == to the others when they denote the same type:
type Box[T] = { value: T, note: string | null }
let t = Reflect.erase(type(Box[Array[int]]))
let boxed = Reflect.valueOf(type(Box[Array[int]]), Box{ value: [1], note: null })
assert(Reflect.type(boxed) == t)
let s = Reflect.shape(t)
if s is Reflect.Record {
assert(s.args[0] == Reflect.erase(type(Array[int])))
assert(s.fields[0].type == s.args[0])
}Reflect.assignable compares two erased types by the assignability rules:
Reflect.assignable(Reflect.erase(type(int)), Reflect.erase(type(int | null))) // true
Reflect.assignable(Reflect.erase(type(int | null)), Reflect.erase(type(int))) // falseErasing keeps the rendering of the type value:
let t = type(int | null)
assert(String.from(Reflect.erase(t)) == String.from(t))Possible issues
Reflect.to takes a typed Reflect.Type[T], never an erased type, since the result needs a static type:
let v = Reflect.valueOf(type(int), 1)
let t = Reflect.erase(type(int))
println(Reflect.to(v, t)) // error: cannot use Reflect.ErasedType where Reflect.Type[T] is expectedA Reflect.Type[T] is not assignable to a Reflect.ErasedType. Write the Reflect.erase call:
let t: Reflect.ErasedType = type(int) // error: cannot use Reflect.Type[int] where Reflect.ErasedType is expectedA union cannot hold an erased type beside another reflection type, since is cannot tell them apart:
let x: Reflect.ErasedType | Reflect.Decl = Reflect.erase(type(int))
// error: a union may hold at most one type with no discriminator, and Reflect.ErasedType and Reflect.Decl both have noneAn erased type has no JSON form:
Json.stringify(Reflect.erase(type(int))) // panic: Json.stringify: a type has no JSON formSee also
Reflect · Reflect.erase · Reflect.Type · Reflect.shape · Reflect.Shape · Reflect.type · Reflect.to · Reflect.assignable
