Skip to content

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.

ant
type Point = { x: int, y: int }
type(Point)                              // Point
type(Array[Point] | null)                // Array[Point] | null
type(Point | null) == type(null | Point) // true

type 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.

ant
type Config = { name: string }
let config = Config{ name: "app" }
let t = type(config) // error: type(...) takes a type, not a value; config is a value

A 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:

ant
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))))
text
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.

ant
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)) // int

Functions by job ​

Type values ​

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:

ant
func listOf[T](t: Reflect.Type[T]): Reflect.Type[Array[T]] {
  Reflect.arrayOf(t)
}

listOf(type(int)) == type(Array[int]) // true

Taking a type apart ​

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 ​

Building a value ​

Types ​

Type values in other namespaces ​

The type value picks the width and the kind of number, and fixes the static type of the result:

ant
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))                // 4

See also ​

Reflect · Types at run time · type · Data formats · Json · Bytes