Reflection
What a type answers about its own make-up while compiling — kind, name, fields, variants, annotations — and how a library writes a derivation from it.
Reflection is a set of questions a type answers while the program is being compiled, and nothing
more. A struct can be asked for its fields, an enum for its variants, and any type for what kind of
type it is. The answers are plain values and descriptors that a for const loop walks, and a body
written over them is checked and unrolled once per type, so the generated code is the code you would
have written by hand. There is no run-time component: no any, no type tags on values, and no table
the program carries to look up a field by name.
Reflection is built from forms described on other pages, and this page brings them together:
| written | answers | covered in |
|---|---|---|
T::Kind, T::Name, T::BaseName | what a type is, and what it is called | below |
T::Target | what a reference, a pointer or a slice points at | targets |
T::Fields, T::Members | the stored parts of a struct, a tuple or a union | fields |
T::Variants | the variants of an enum | variants |
v.(f), f::Type | one field of a value, and its type | walking fields |
x is c, x.(c), c::Payload, T::Make(c, p) | testing, reading and building a variant | variants |
T::Partial, c::Partial, T::Make(p) | building a value a field at a time | partial values |
if const T: Trait | what a type can do | asking for a trait |
@annotation, f::Annotation[A] | a program’s own marks on a field, variant or type | annotations |
The other :: attributes — Color::First, Int::Max, T::Min — are on
attributes. for const and if const are on
generics.
What a type is, and what it is called
T::Kind is a value of the standard enum Kind, with one variant for each structure a type can
have: Struct, Tuple, SimpleEnum, DataEnum, Union, Array, Vector, Slice, Int,
Float, Fixed, Bool, Char, String, Unit, Ref, Weak, Pointer, Fn, Trait and
Opaque. A subtype has the kind of the type it narrows, a bitfield struct is a Struct, and a
closure, which is reached through a reference, is a Ref.
struct Point
x: int
y: int
enum Light
Red
Green
enum Shape
Dot
Circle(r: int)
union Bits
i: u32
f: f32
type Pair = (int, real)
type Four = [4]int
print(Kind::Image(Point::Kind), Kind::Image(Light::Kind), Kind::Image(Shape::Kind), Kind::Image(Bits::Kind))
print(Kind::Image(Pair::Kind), Kind::Image(Four::Kind), Kind::Image(string::Kind))
print(Kind::Image(f64::Kind), Kind::Image(bool::Kind), Kind::Image(unit::Kind))
Struct SimpleEnum DataEnum Union
Tuple Array String
Float Bool Unit
Kind is an ordinary enum, so it compares with == and is the usual condition of an if const:
if const T::Kind == Kind.Struct keeps one arm of a body and discards the rest, for each type.
T::Name is the type as a program spells it, with its arguments, and T::BaseName is the
declared name alone, which is what Debug and Display print in front of a value’s parts:
struct Box[T]
v: T
type Pair = (int, real)
print(Box[int]::Name, Box[int]::BaseName)
print(Pair::Name, int::Name)
Box[int] Box
(int, real) int
A type is named as it was written, by the rule a
diagnostic follows: u16 and ushort are both
the type’s own names, so each answers as written; an alias answers as its declaration spelled what it
names, and a field walk’s f::Type as the struct declared the field. A type parameter is named
canonically, because one instantiation serves every spelling of its argument:
struct Box[T]
v: T
type Meters = u16
name_of[T]() -> string = T::Name
print(u16::Name, ushort::Name, u8::Name, Meters::Name, Box[u8]::Name)
print(name_of[u16](), name_of[Meters]())
u16 ushort u8 u16 Box[u8]
ushort ushort
T::BaseName is refused where the type has no declared name, such as a tuple, and Box::Name with
no arguments is refused because Box alone is not a single type; both are shown under
what it refuses. An anonymous struct or union block answers with the keyword
that opened it.
A trait answers these three questions too, as Rust’s type_name answers for dyn View: its kind
is Trait, and its names are the ones it was declared under. A trait object standing in for a type
parameter answers the same, so if const T::Kind == Kind.Trait tells an object from a type. Behind
a mode it is the mode that answers: &View is a Ref and *View a Pointer, and
T::Target is what asks about the trait behind it.
trait View
draw(self) -> int
trait Get[T]
get(self) -> T
struct Button
width: int
impl View for Button
draw(self) -> int = self.width
which[T: View]() -> string
if const T::Kind == Kind.Trait then "object" else T::Name
print(Kind::Image(View::Kind), View::Name, View::BaseName)
print(Get[int]::Name, Get[int]::BaseName)
print(which[View](), which[Button]())
Trait View View
Get[int] Get
object Button
A trait has no layout, so View::Fields, View::Members and sizeof(View) are refused, as a trait
is as a field, a local or a parameter:
trait View
draw(self) -> int
print(View::Fields.len)
'View' is a trait, so it describes behaviour rather than a layout — write '*View' or '&View' for a trait object, or bound a type parameter with '[T: View]'
What a reference points at
T::Target is the type a reference or a pointer points at, named after Rust’s Deref::Target.
&T, &sync T, *T and weak T answer T, and a slice []T, which is a pointer and a count,
answers its element. It is a type rather than a value, so it is written wherever a type is (sizeof,
a type argument, a binding’s type) or asked a question of its own. That is how a body tells a trait
object from a struct behind the same mode: T::Kind is Ref for both, and T::Target::Kind is
Trait for one and Struct for the other.
trait View
draw(self) -> int
struct Button
width: int
impl View for Button
draw(self) -> int = self.width
which[T]() -> string
if const T::Target::Kind == Kind.Trait then "object " + T::Target::Name
else "type " + T::Target::Name
type Flags = []bool
print(which[&View](), which[*Button](), which[&sync Button]())
print(Flags::Target::Name, sizeof(Flags::Target))
object View type Button type Button
bool 1
A mode can be written in parentheses on the left of ::, so a question needs no alias or type
parameter, and a projection chains, one level per Target:
trait View
draw(self) -> int
struct Button
width: int
print(Kind::Image((&View)::Kind), Kind::Image((&View)::Target::Kind))
print((**Button)::Target::Name, (**Button)::Target::Target::Name)
Ref Trait
*Button Button
The type grammar reads the same spelling, so (&Button)::Target stands as a binding’s type, a
parameter’s, a type argument and sizeof‘s operand. The parentheses are what group the mode for the
::: &Button::Target would be a reference to Button::Target. A tuple type (A, B) and () keep
their meaning, and one type in parentheses with no :: after it is still refused as no tuple.
An associated function is called through the projection as it is through the type’s own name, and in a walk over the fields the call reaches each field’s own referent:
struct Button
width: int
impl Button
make() -> Button = Button(4)
struct Panel
width: int
impl Panel
make() -> Panel = Panel(30)
struct Window
button: &Button
panel: *Panel
widths[T]() -> int
var sum = 0
for const f in T::Fields
sum += f::Type::Target.make().width
sum
wide(b: (&Button)::Target) -> int = b.width * 2
val b: (&Button)::Target = (&Button)::Target.make()
val o: Option[(**Panel)::Target::Target] = Some(Panel(5))
print(wide(b), o.unwrap().width, sizeof((&Button)::Target) == sizeof(Button), widths[Window]())
8 5 true 34
A type that points at nothing has no Target, as Rust’s <i32 as Deref>::Target is refused. That
includes a trait object, which is where a chain ends, and an array, which holds its elements rather
than pointing at them:
print(int::Target::Name)
'int' is an integer, which points at nothing, so 'int::Target' names no type — 'Target' is what a reference, a pointer, a weak reference or a slice points at
A trait’s own associated type named Target wins, as a program’s names win over every form the
compiler supplies, and it is reached through a reference as any associated type is: where
impl Holds for Jar says type Target = u8, Jar::Target and (&Jar)::Target are both u8.
Every question is asked through an associated type as it is through Target: M::Item::Name,
M::Item::Kind, M::Item::Fields, sizeof(M::Item), an attribute with an operand and a further
::Target all read the type the projection names. Off a declared type, ::Name answers as the
implementation wrote type Item = …, an alias by its declaration as the alias’s own name does; off a
type parameter it is canonical, as the parameter is:
trait Has
type Item
type Meters = u16
struct Point
x: int
y: u8
struct Shelf
n: int
impl Has for Shelf
type Item = Meters
struct Plot
n: int
impl Has for Plot
type Item = &Point
asked[M: Has]() -> string = s"${M::Item::Name} ${Kind::Image(M::Item::Kind)}"
print(Shelf::Item::Name, Shelf::Item::Max, Plot::Item::Target::Name, sizeof(Plot::Item::Target))
print(asked[Shelf](), asked[Plot]())
for const f in Plot::Item::Target::Fields
print(f.name, f::Type::Name)
u16 65535 Point 8
ushort Int &Point Ref
x int
y u8
A left side that is a value keeps the refusal it always had, and a subject with no implementation is
told so by the projection: int::Item::Name says that int implements no trait declaring Item.
A type parameter bounded by two traits that each declare Item is refused, since an associated type
is named without its trait: M::Item says it is ambiguous and names both traits. A trait both bounds
require is one trait, and is not ambiguous. A trait asked for its layout (Has::Max) is told that it
describes behaviour rather than a layout.
Fields and members
T::Fields lists the named fields of a struct, a tuple or a union, in declaration order. Each
element is a sysl.reflect.Field, a plain value that exists while compiling:
| member | is |
|---|---|
name: string | the field’s name; a tuple’s parts are "0", "1", and so on |
index: usize | its position in T::Fields, counting from 0 |
offset: usize | where it begins, in bytes from the start of the value |
public: bool | whether code outside the type’s own file may name it |
struct Account
id: int
private secret: u8
owner: string
type Pair = (int, real)
for const f in Account::Fields
print(f.name, f.index, f.offset, f.public)
for const f in Pair::Fields
print(f.name, f.index, f.offset)
id 0 0 true
secret 1 4 false
owner 2 8 true
0 0 0
1 1 8
A union’s fields all begin at offset 0. T::Fields.len is the number of fields, a constant, and
the list is a for const subject like any other.
T::Members lists everything a struct, tuple or union stores: its named fields, each anonymous
struct or union block, and each unnamed _ bitfield. A zero-width _ stores nothing and is not
listed. Its elements are sysl.reflect.Members, with a Field‘s four members plus
anonymous: bool, which is true for a block. A block’s name is "", and an unnamed bitfield’s
name is "_".
struct Packet
tag: u8
union
word: u32
real: f32
lo: u4 in u32
_: u4 in u32
hi: u8 in u32
print(Packet::Members.len, Packet::Fields.len)
for const m in Packet::Members
print(m.index, m.name, m.anonymous)
for const f in Packet::Fields
print(f.index, f.name)
5 3
0 tag false
1 true
2 lo false
3 _ false
4 hi false
0 tag
1 lo
2 hi
T::Fields holds only the named fields, so a derived Eq or Hash never reads an unnamed
bitfield, and an anonymous block is not a field in its own right. T::Members is what Debug walks,
since it shows every part a value stores. Each list numbers its own elements: a Field‘s index
is its position in T::Fields and a Member‘s is its position in T::Members, which is why lo is
field 1 and member 2 above. So f.index > 0 means “not the first field”, whatever members come
before it.
A member that is an anonymous block is read and assigned as a struct or union of its own, and its
::Type says which:
struct Packet
tag: u8
union
word: u32
real: f32
for const m in Packet::Members
if const m.anonymous
print(Kind::Image(m::Type::Kind), m::Type::Fields.len)
Union 2
Walking the fields
for const f in T::Fields unrolls once per field, and f is a compile-time descriptor in each
copy. v.(f) selects that field of a value v of type T; it has the field’s type and can be
read and assigned like v.x. Each copy is checked separately, so one line of code covers fields of
different types:
struct Point
x: int
y: real
twice[T](v: T) -> T
var out = v
for const f in T::Fields
out.(f) = v.(f) + v.(f)
out
val p = twice(Point(3, 1.5))
print(p.x, p.y)
6 3
A copy that the field’s type does not support is refused, and the diagnostic names that copy: twice
over a struct holding a bool reports the error for that field only.
f::Type stands for the field’s type wherever a type is written. A type is never a value; this
is how a generic is called, or a local declared, once per field:
struct Point
x: int
y: real
zero[T]() -> T
var out: T
for const f in T::Fields
var tmp: f::Type
out.(f) = tmp
out
val p = zero[Point]()
print(p.x, p.y)
0 0
It stands before . the way an alias does, so an associated function is called through it —
a trait’s, reached through the field’s type, or one the type declares itself. c::Payload does
the same for a variant’s payload:
trait Make
make(n: int) -> Self
impl Make for int
make(n: int) -> int = n * 10
impl Make for string
make(n: int) -> string = s"#$n"
struct Row
id: int
label: string
for const f in Row::Fields
print(f.name, f::Type.make(4))
id 40
label #4
Each copy asks its own field’s type, so a type without the member is refused in that copy alone:
trait Make
make(n: int) -> Self
impl Make for int
make(n: int) -> int = n
struct Row
id: int
on: bool
for const f in Row::Fields
print(f::Type.make(4))
bool has no associated function 'make' (in the copy for field 'Row.on')
A value is built with var out: T and one assignment per field. Like any var, out starts at
the type’s zero value, so an unassigned field keeps its zero; a type with no zero value is refused at
the var.
Variants
T::Variants lists the variants of an enum, simple or data, in declaration order. Each is a
sysl.reflect.Variant, with name: string and index: usize, and for const c in T::Variants
unrolls once per variant. A variant’s payload is described by c::Fields, using the same
descriptors as a struct, so it is walked the same way:
enum Shape
Dot
Circle(r: int)
Rect(w: int, h: int)
for const c in Shape::Variants
print(c.name, c.index, c::Fields.len)
for const c in Shape::Variants
for const f in c::Fields
print(c.name, f.name, f.index)
Dot 0 0
Circle 1 1
Rect 2 2
Circle r 0
Rect w 0
Rect h 1
An encoder and a decoder need three operations on a value, and none of them requires a match:
x is cis true whenxholds that variant. It is a run-time test in each copy, so the walk finds the one that matches.x.(c)is the payload, and it traps whenxholds another variant. The payload isunitfor a variant with no fields, the field itself for a variant with one, and a tuple for more.T::Make(c, p)builds the variantcofTfrom a payloadpof that same shape.
enum Shape derives Eq
Dot
Circle(r: int)
Rect(w: int, h: int)
describe(s: Shape) -> string
var out = ""
for const c in Shape::Variants
if s is c then out = c.name
out
radius(s: Shape) -> int
for const c in Shape::Variants
if const c.name == "Circle"
if s is c then return s.(c)
-1
print(describe(Shape.Rect(2, 3)), describe(Shape.Dot))
print(radius(Shape.Circle(5)), radius(Shape.Dot))
Rect Dot
5 -1
Reading a variant from a value that holds a different one stops the program, after writing
variant 'Shape.Circle' was asked for, and the value is 'Shape.Dot' (main.sysl:9:18) to standard error,
as a failed bounds check does:
enum Shape
Dot
Circle(r: int)
val s = Shape.Dot
for const c in Shape::Variants
if const c.name == "Circle"
print(s.(c))
c::Payload stands for a variant’s payload type wherever a type is written. A decoder builds a
variant with one generic body: declare the payload, assign each field of c::Fields with p.(f), and
pass it to T::Make. For a payload of one field, p.(f) is p itself, and is assignable:
enum Shape derives Eq
Dot
Circle(r: int)
Rect(w: int, h: int)
build(i: usize) -> Shape
var out = Shape.Dot
for const c in Shape::Variants
if c.index == i
var p: c::Payload
for const f in c::Fields
p.(f) = 10 * int(f.index + 1)
out = Shape::Make(c, p)
out
print(build(0) == Shape.Dot, build(1) == Shape.Circle(10), build(2) == Shape.Rect(10, 20))
true true true
A simple enum is walked the same way: its variants have no payload, and x is c and c.name are all
a serializer needs.
Building a value a field at a time
var p: c::Payload works above because every field is an int, which starts at zero. A field that is
a Buf, a Map, a &T or an enum that carries data has no zero, so a value holding one cannot be
declared first and filled in later. A partial value is the type’s fields held one at a time:
T::Partialis, for a structT, a tuple with oneOptionper field, in the fields’ order;c::Partialis the same for the payload of the variantc. Like a payload, a partial value of one field is that field’sOption, and one of no fields isunit.- Its zero is every field
None, whatever the fields are, sovar p: T::Partialneeds no initial value. p.(f)reads and writes fieldfof it as anOption[f::Type], for the samefthatv.(f)selects with; being a tuple, it is also reached by position,p.0.T::Make(p)builds the struct, andT::Make(c, p)builds the variantc, taking each field out of itsOption.T::Make(c, payload)still takes the payload itself.
import sysl.buf.{Buf, buf}
enum Expr
Lit(n: int)
Add(a: &Expr, b: &Expr)
struct Rec
name: string
items: Buf[int]
root: &Expr
fill[T](given: Buf[string]) -> T
var p: T::Partial
for const f in T::Fields
if const f.name == "name"
p.(f) = Some(given[0])
else if const f.name == "items"
var b: Buf[int] = buf()
b.push(7)
p.(f) = Some(b)
else if const f.name == "root"
p.(f) = Some(Expr.Add(Expr.Lit(1), Expr.Lit(2)))
T::Make(p)
show() -> string
var given: Buf[string] = buf()
given.push("rec")
val r = fill[Rec](given)
s"${r.name} ${r.items[0]} ${*r.root is Add(_, _)}"
print(show())
rec 7 true
A field still None when the value is made stops the program, as a bounds check does, naming
the type and the field and writing the location to standard error. The value would otherwise hold a
field nothing was given for; a decoder reports a missing field itself before it gets that far.
struct P
x: int
y: int
var p: P::Partial
p.0 = Some(1)
print(P::Make(p).x)
The program stops with 'P' was made from a partial value whose field 'P.y' is None (main.sysl:8:8). Where the value is made while compiling (a const), the compilation stops with
the same words.
A partial value is a plain tuple: it is copied, returned and passed like one, and over scalar fields it
allocates nothing, so it may be used in a @no_alloc body. A field the file could not name — a
private one of another file’s struct — cannot be reached through p.(f), and T::Make(p) of such a
struct is refused there, since building it writes every field. A struct with a Drop comes out of
T::Make(p) as the &T its constructor makes.
Only a struct, or a variant’s payload, has a partial value. An enum is built a variant at a time, a union holds one field at a time, an opaque struct’s fields are not known outside its module, and a struct with an anonymous member or an unnamed bitfield holds storage no field names; each is refused:
enum Shape
Circle(r: int)
Dot
var s: Shape::Partial
'Shape::Partial' names nothing: an enum is built a variant at a time, and a variant's partial value is 'c::Partial', for the 'c' of a 'for const c in Shape::Variants'
A trait’s associated type named Partial wins: where T implements a trait declaring type Partial, T::Partial is that type, as a name a program declares beats every form the compiler
supplies.
Asking what a type can do
if const T: Trait is true when T implements Trait, written as a bound is written:
T: Display + Hash, T: Conv[int] and f::Type: Display are all valid. It is an operand of an
if const or elif const, so it combines with &&, ||, ! and ordinary constants. A serializer
uses it to choose between a type’s own implementation and walking its fields:
trait Named
name(self) -> string
impl Named for int
name(self) -> string = "int"
struct Pin
on: bool
describe[T]() -> string
if const T: Named
"named"
elif const T: Display
"displayable"
else
"plain"
print(describe[int](), describe[string](), describe[Pin]())
named displayable plain
&& and || decide from their left operand where it settles the answer, and the right is then
not read, as at run time it is not run. So a test guards an operand that depends on it, the way a
nested if const guards its arm: at a type the test refuses, a call that needs the bound is never
checked.
wide[T: Integer]() -> bool = sizeof(T) > 4
size[T]() -> string = if const T: Integer && wide[T]() then "wide" else "narrow or not a number"
print(size[u64](), size[u8](), size[bool]())
wide narrow or not a number narrow or not a number
Annotations
A program may declare an annotation: a struct written under @annotation, whose fields are the
arguments each use supplies. It may then be placed above a field, a variant or a type, and reflection
reads it: f::Annotation[A], c::Annotation[A] and T::Annotation[A] answer an Option[A],
which is None where the annotation is absent. The built-in attributes remain reserved; an annotation
is always one the program declared.
@annotation
struct label
text: string
@annotation
struct secret
level: int
@label("a shape")
enum Shape
@label("nothing") Dot
Circle(r: int)
struct User
@label("the id") id: int
@secret(2) password: string
name: string
for const f in User::Fields
print(f.name, f::Annotation[label].map(l -> l.text).unwrap_or("-"), f::Annotation[secret].is_some())
for const c in Shape::Variants
print(c.name, c::Annotation[label].map(l -> l.text).unwrap_or("-"))
print(Shape::Annotation[label].map(l -> l.text).unwrap_or("-"), User::Annotation[label].is_some())
id the id false
password - true
name - false
Dot nothing
Circle -
a shape false
The arguments are checked against the annotation’s fields as a call’s arguments are. A name that is not an annotation is refused, and so is an annotation written twice above the same declaration:
@annotation
struct label
text: string
struct S
@label(5) a: int
'text' of 'label' is string, but int was given
struct S
@label("x") a: int
'@label' names no annotation
@annotation
struct label
text: string
struct S
@label("a") @label("b") a: int
'@label' is written twice above 'a', and it says nothing the once does not
An annotation of the field’s type
An annotation may take one type parameter, and each use binds it to the type of the field it is
written on. @default(8080) port: u16 is the construction default[u16](8080), so 8080 is checked
as a u16 and @default("localhost") host: string as a string — one annotation carries a value of
every field’s own type. Reflection asks for it by its bare name: f::Annotation[default] answers an
Option[default[f::Type]], whose v has the field’s type. Written with its argument,
f::Annotation[default[f::Type]] reads the same thing.
@annotation
struct default[F]
v: F
enum Mode
Fast
Slow
struct Point
x: int
y: int
struct Cfg
@default(8080) port: u16
@default("localhost") host: string
@default(Mode.Slow) mode: Mode
@default(Some(3)) retries: Option[int]
@default(Point(1, 2)) origin: Point
name: string
for const f in Cfg::Fields
val d = f::Annotation[default]
if d.is_some() then print(f.name, d.unwrap().v) else print(f.name, "-")
port 8080
host localhost
mode Mode.Slow
retries Some(3)
origin Point(x: 1, y: 2)
name -
On a field of a generic declaration the parameter is the field’s type in the declaration’s own terms:
@default(None) x: Option[T] is checked as an Option[T] where it is written, and read at each
instantiation — Wrap[int]::Fields answers a default[Option[int]]. The parameter may carry a bound
(struct shown[F: Display]), which each use’s field type must meet.
A value that is not of the field’s type is refused at the use, naming the field:
@annotation
struct default[F]
v: F
struct Cfg
@default("x") port: u16
'@default' is written on 'port', of type 'ushort', so its 'F' is 'ushort' and what its parentheses say must be of that type
Only a field has a type to give the parameter, so a generic annotation above a type declaration or a variant is refused, and so is reading one off something that is not a field:
@annotation
struct default[F]
v: F
@default(1)
struct Cfg
port: u16
'@default' is generic — its 'F' is the type of the field it is written on — and the declaration 'Cfg' is not a field, so there is no type to give it
An annotation takes at most one type parameter, and it is a type, never a const value:
@annotation
struct pair[A, B]
a: A
an annotation takes at most one type parameter — a use of '@pair' gives it the type of the field it is written on, which is one type, and nothing gives the others
Privacy
A query lists every field, and f.public says which are public. v.(f) on a private field is
allowed only in the type’s own file, or in a derivation the type requested with derives: the
type’s author opts in by writing the clause, and all other code sees only the public fields.
// secrets/main.sysl
module secrets
struct Secret
shown: int
private hidden: int
dump[T](v: T) -> string
var out = ""
for const f in T::Fields
out = out + s"${v.(f)}${if f.public then "" else "!"} "
out
print(dump(Secret(shown = 1, hidden = 2)))
1 2!
Inside the file that declares the type, the walk reaches hidden. A generic written in any other
file is refused at the copy for that field, with the field named, and the same holds for
T::Members.
Generic types and opaque ones
A generic type is reflected as an instance. Box[int]::Fields lists the fields of that instance,
with int for v. Box::Fields with no arguments is refused, as Box::First would be, because
there is no single type to ask.
struct Box[T]
v: T
n: int
for const f in Box[string]::Fields
print(f.name, f.offset)
v 0
n 24
An opaque struct that has fields is a struct within its own module. There, and in a derivation
it requested, T::Kind is Struct and T::Fields and T::Members list its fields; anywhere else it
is Opaque and has none. An opaque struct with no body is Opaque everywhere.
// lib/main.sysl
module lib
opaque struct H
x: int
private y: u8
opaque struct Bare
print(Kind::Image(H::Kind), Kind::Image(Bare::Kind))
for const m in H::Members
print(m.name, m.offset)
Struct Opaque
x 0
y 4
From another module the same H is Opaque and H::Fields is refused.
There is no run-time reflection
Every query is answered at compile time. A descriptor is plain data, so a program may place a
type’s name or a table of its field names in the object file (print(T::Name) prints a string
constant), but there is nothing to ask of a value at run time. There is no any type, a trait object
carries no description of the type behind it, and a program that does not use reflection pays nothing
for it. A type’s methods and a module’s declarations are not reflected either, since the methods
available on a type depend on what is imported.
Writing a derivation
A trait may carry a derive member, a body written with the queries above, and a derives clause
instantiates it for a type as the impl block you would otherwise write by hand. Everything the
standard library derives (Eq, Ord, Hash, Display, Debug) is written this way, and a
library’s own trait is derived in exactly the same way. The clause, the rules the body must follow,
and how a field that cannot support the derivation is refused are described on
traits.
The program below is a complete serializer. Json is declared with an encode member and a
derivation of it written with reflection. A struct is walked field by field; a simple enum is written
as its variant’s name with T::Image(self) (in the enum’s copy, T is the enum); @rename changes a
key; and a field’s own encode is reached through self.(f).encode(w):
@annotation
struct rename
name: string
trait Json
encode(self, w: *Writer)
derive[T] encode(self: T, w: *Writer)
if const T::Kind == Kind.Struct
w.write("{".bytes)
for const f in T::Fields
if f.index > 0 then w.write(",".bytes)
val key = f::Annotation[rename].map(r -> r.name).unwrap_or(f.name)
w.write(s"\"$key\":".bytes)
self.(f).encode(w)
w.write("}".bytes)
elif const T::Kind == Kind.SimpleEnum
w.write(s"\"${T::Image(self)}\"".bytes)
else
@assert(false, s"Json cannot derive for ${T::Name}")
impl Json for int
encode(self, w: *Writer) = w.write(s"$self".bytes)
impl Json for string
encode(self, w: *Writer) = w.write(s"\"$self\"".bytes)
enum Role derives Json
Admin
Guest
struct Address derives Json
city: string
zip: int
struct User derives Json
@rename("id") user_id: int
name: string
role: Role
home: Address
User(7, "ann", Role.Admin, Address("Oslo", 150)).encode(stdout())
print("")
{"id":7,"name":"ann","role":"Admin","home":{"city":"Oslo","zip":150}}
User gets an impl Json for User whose body is four unrolled field writes, and self.(f).encode(w)
is checked for each one, so a field whose type does not implement Json is refused at the derives
clause, with the field named. For a type the derivation does not handle, the library supplies its own
message: the @assert‘s computed message is reported where the type requested the derivation.
trait Named
name(self) -> string
derive[T] name(self: T) -> string
if const T::Kind == Kind.Struct
T::BaseName
else
@assert(false, s"Named cannot derive for ${T::Name}, which is not a struct")
""
enum Light derives Named
Red
Green
assertion failed: Named cannot derive for Light, which is not a struct
What it refuses
Each refusal names the type and what it is, or the descriptor and what it answers:
for const f in int::Fields
print(f.name)
'int::Fields' lists the fields of a struct, a tuple or a union, and 'int' is an integer
struct Box[T]
v: T
print(Box::Name)
'Box' is generic, so 'Box::Name' has no single type to read — write the arguments, 'Box[…]::Name'
A descriptor answers only for its own kind of thing: a field has a ::Type, and a variant has
::Fields, ::Payload and ::Partial.
struct P
x: int
for const f in P::Fields
var a: f::Payload
'f' is the descriptor 'P.x', which answers '::Type', the field's type, and not '::Payload'
enum E
A(x: int)
for const c in E::Variants
var a: c::Type
'c' is the descriptor 'E.A', which answers '::Fields', its payload's fields, '::Payload', its payload's type, and '::Partial', its payload's partial value, and not '::Type'
A name a type does not answer is refused with the type named:
struct P
x: int
print(P::Bogus)
'P' has no type attributes