Reflection
Reflection in Ant starts from a type the program writes, never from a value. The type expression type(T) makes the type T a value of type Reflect.Type[T]. The Reflect namespace takes such a value apart one level at a time, boxes a value together with its type, reads a box along that type, and builds new values from boxed parts. With these, a typed decoder or encoder for a data format is ordinary library code. A few functions elsewhere take a type value too, to choose what to read or write.
type Point = { x: int, y: int }
type(Point) // Point
type(Array[Point] | null) // Array[Point] | null
type(Point | null) == type(null | Point) // truetype takes a type, never a value: unlike Python's type(x) or JavaScript's typeof x, type(config) on a variable is a compile error.
type Config = { name: string }
let config = Config{ name: "app" }
let t = type(config) // error: type(...) takes a type, not a value; config is a valueA function that walks a type works on Reflect.ErasedType, since the type of a child is not known statically one level down. Reflect.erase turns the caller's Reflect.Type[T] into a Reflect.ErasedType at the start, giving up its static type:
type Point = { x: int, y: int }
type Route = { name: string, points: Array[Point] }
func describe(t: Reflect.ErasedType): string {
let s = Reflect.shape(t)
if s is Reflect.Record {
return s.fields.map({ "${x.name}: ${describe(x.type)}" }) | String.join(", ")
}
if s is Reflect.Array {
return "list of (${describe(s.element)})"
}
String.from(t)
}
println(describe(Reflect.erase(type(Route))))name: string, points: list of (x: int, y: int)A box keeps the type it was made at, resolved to one member when that type is a union, and Reflect.to gives the value back at a static type that the caller's type(T) fixes. It panics when the box's type is not assignable to T.
type Point = { x: int, y: int }
let d = Reflect.valueOf(type(Point), Point{ x: 1, y: 2 })
Reflect.field(d, "y") // Reflect.Value(2: int)
Reflect.to(d, type(Point)) // Point{ x: 1, y: 2 }
Reflect.type(Reflect.valueOf(type(int | null), 42)) // intFunctions by job
Type values
- type:
type(T), the typeTas a value - Reflect.erase: a type value with its static type forgotten
- Reflect.arrayOf:
Array[T]from a type value forT - Reflect.setOf:
Set[T]from a type value forT - Reflect.mapOf:
Map[K, V]from type values forKandV - Reflect.nullable:
T | nullfrom a type value forT
Inside a generic function type(Array[T]) is a compile error, since T is not a closed type, so the function builds the type from its Reflect.Type[T] parameter:
func listOf[T](t: Reflect.Type[T]): Reflect.Type[Array[T]] {
Reflect.arrayOf(t)
}
listOf(type(int)) == type(Array[int]) // trueTaking a type apart
- Reflect.shape: one level of a type's structure, as a
Reflect.Shape - Reflect.assignable: whether a value of one type may go where another is expected
Boxing and unboxing
- Reflect.valueOf: a value boxed with its type
- Reflect.to: the value in a box, at a static type
- Reflect.type: the type of a box, never a union or an alias
- Reflect.same: whether two boxes hold the same record, array, map, set, or
Bytes, and==for any other value
Reading a box
- Reflect.scalar: the leaf value in a box
- Reflect.items: an array's elements or a set's members, boxed
- Reflect.entries: a map's entries, boxed
- Reflect.field: one field of a record, boxed
- Reflect.payload: the base value of a named primitive type, boxed
Building a value
- Reflect.makeScalar: a leaf boxed at a scalar type, or
nullwhen it does not fit - Reflect.makeArray: a new array from boxed elements
- Reflect.makeSet: a new set from boxed members
- Reflect.makeMap: a new map from boxed entries
- Reflect.makeRecord: a new record from boxed fields, the rest from their defaults
- Reflect.makeTagged: a new value of a named primitive type
Types
- Reflect.Type: a type value whose static type is known
- Reflect.ErasedType: a type value whose static type is not known
- Reflect.Value: a value together with a type it belongs to
- Reflect.Shape: what
Reflect.shapereturns, one member per kind of type - Reflect.Field: one field of a record shape
- Reflect.Decl: the declaration of a named type, shared by all its instantiations
- Reflect.Scalar: what
Reflect.scalarreturns
Type values in other namespaces
- Bytes.read: a number of the given type from a byte buffer
- Bytes.write: overwrite bytes with a number of the given type
- Bytes.append: append a number of the given type
- Bytes.sizeOf: how many bytes a number type takes
The type value picks the width and the kind of number, and fixes the static type of the result:
let b = Bytes.new()
b.append(type(uint16), uint16(513), Bytes.BigEndian{})
b // Bytes(02 01)
b.read(type(uint16), 0, Bytes.BigEndian{}) // 513
Bytes.sizeOf(type(float32)) // 4See also
Reflect · Types at run time · type · Data formats · Json · Bytes
