Reflect.Decl
type Reflect.DeclReflect.Decl is a named type's declaration without its type arguments: Box[int] and Box[string] share one. Two are == when they are the same declaration, and one renders as the declared name. It is the key for anything that applies to every instantiation of a generic type.
Details
- No literal or constructor makes a
Reflect.Decl. You read one from thedeclfield of aReflect.Record,Reflect.Tagged, orReflect.Opaqueshape. An anonymous record type has none, so itsdeclisnull. - It compares by identity and hashes, so it works as a
Mapkey orSetmember, and does nothing else. It renders as the name written aftertype, whatever name an import gives the type; a prelude declaration renders with its owner, asPanic.Frame. iscannot test it, andJson.stringifypanics on it.
Basic examples
Read the declaration of a record type from its shape:
type Box[T] = { value: T }
let s = Reflect.shape(Reflect.erase(type(Box[int])))
if s is Reflect.Record {
println(s.decl)
}BoxBox[int] and Box[string] are different types with one declaration:
type Box[T] = { value: T }
func declOf(t: Reflect.ErasedType): Reflect.Decl | null {
let s = Reflect.shape(t)
if s is Reflect.Record || s is Reflect.Tagged || s is Reflect.Opaque { s.decl } else { null }
}
let ints = Reflect.erase(type(Box[int]))
let strings = Reflect.erase(type(Box[string]))
println(ints == strings)
println(declOf(ints) == declOf(strings))false
trueScope
Every named type has a declaration: a named record type, a named primitive type, or an opaque type, from your program or from the prelude. Types with no declaration, such as int, an anonymous record type, or a union such as Result[int, Error], give null; each member of a union has its own:
type Box[T] = { value: T }
type Id[T] = string
type User = { id: Id[User], name: string }
func declOf(t: Reflect.ErasedType): Reflect.Decl | null {
let s = Reflect.shape(t)
if s is Reflect.Record || s is Reflect.Tagged || s is Reflect.Opaque { s.decl } else { null }
}
println(declOf(Reflect.erase(type(Box[float64]))))
println(declOf(Reflect.erase(type(Id[User]))))
println(declOf(Reflect.erase(type(Channel[int]))))
println(declOf(Reflect.erase(type(Panic.Frame))))
println(declOf(Reflect.erase(type(Time.Instant))))
println(declOf(Reflect.erase(type({ x: int, y: int }))))
println(declOf(Reflect.erase(type(int))))
println(declOf(Reflect.erase(type(Result[int, Error]))))
println(declOf(Reflect.erase(type(Ok[int]))))Box
Id
Channel
Panic.Frame
Time.Instant
null
null
null
OkA type declared in a block has one declaration for the whole run, however often the block runs:
func configType(): Reflect.ErasedType {
type Config = { name: string }
Reflect.erase(type(Config))
}
let a = Reflect.shape(configType())
let b = Reflect.shape(configType())
if a is Reflect.Record && b is Reflect.Record {
println(a.decl == b.decl)
}trueAn import renames the type in your source, not its declaration, which renders as the name written after type:
export type Circle = { r: float64 }import "./geo.ant" { Circle: Round }
import "./geo.ant" Geo
let a = Reflect.shape(Reflect.erase(type(Round)))
let b = Reflect.shape(Reflect.erase(type(Geo.Circle)))
if a is Reflect.Record && b is Reflect.Record {
println(a.decl)
println(a.decl == b.decl)
}Circle
trueApplications
Count how many instantiations of each generic type a list of types holds:
type Box[T] = { value: T }
type Pair[A, B] = { first: A, second: B }
let types = [
Reflect.erase(type(Box[int])),
Reflect.erase(type(Pair[int, string])),
Reflect.erase(type(Box[string])),
Reflect.erase(type(Box[Box[int]])),
]
let counts: Map[Reflect.Decl, int] = Map{}
for t in types {
let s = Reflect.shape(t)
let decl = if s is Reflect.Record { s.decl } else { null }
if decl == null {
continue
}
counts[decl] = (counts.get(decl) ?? 0) + 1
}
println(counts)Map{ Box: 3, Pair: 1 }Give every instantiation of a generic type one encoding. Here every Box[T], whatever its T, encodes as its value, and every Id[T] as its string:
type Box[T] = { value: T }
type Id[T] = string
type User = { id: Id[User], name: Box[string], age: Box[int] }
func declOf(t: Reflect.ErasedType): Reflect.Decl | null {
let s = Reflect.shape(t)
if s is Reflect.Record || s is Reflect.Tagged || s is Reflect.Opaque { s.decl } else { null }
}
let transparent = Set.of([
declOf(Reflect.erase(type(Box[int]))),
declOf(Reflect.erase(type(Id[int]))),
])
func encode(d: Reflect.Value): string {
let t = Reflect.type(d)
let s = Reflect.shape(t)
if transparent.has(declOf(t)) {
if s is Reflect.Tagged {
return encode(Reflect.payload(d))
}
return encode(Reflect.field(d, "value"))
}
if s is Reflect.Record {
return s.fields.map({ "${x.name}=${encode(Reflect.field(d, x.name))}" }) | String.join(" ")
}
String.from(Reflect.scalar(d))
}
let ann = User{ id: Id("u-1"), name: Box{ value: "Ann" }, age: Box{ value: 31 } }
println(encode(Reflect.valueOf(type(User), ann)))id=u-1 name=Ann age=31Properties and relations
The declaration ignores type arguments, which the shape keeps in args. String.from gives the declared name:
type Pair[A, B] = { first: A, second: B }
let s = Reflect.shape(Reflect.erase(type(Pair[int, string])))
let t = Reflect.shape(Reflect.erase(type(Pair[bool, bool])))
if s is Reflect.Record && t is Reflect.Record {
assert(s.decl == t.decl)
assert(s.args != t.args)
assert(String.from(s.decl) == "Pair")
}Possible issues
A name is not unique. Two blocks may each declare a Config: the declarations render alike and are not ==. Key a table by the Reflect.Decl, not by its name:
func first(): Reflect.ErasedType {
type Config = { name: string }
Reflect.erase(type(Config))
}
func second(): Reflect.ErasedType {
type Config = { name: string }
Reflect.erase(type(Config))
}
let a = Reflect.shape(first())
let b = Reflect.shape(second())
if a is Reflect.Record && b is Reflect.Record {
println("${a.decl} ${b.decl}")
println(a.decl == b.decl)
}Config Config
falseis cannot test a Reflect.Decl. Narrow a union that holds one by testing its other members:
func describe(d: Reflect.Decl | int): string {
if d is Reflect.Decl { "a declaration" } else { "a number" }
// error: is cannot test Reflect.Decl; narrow by testing the other members
}A declaration has no JSON form. Write its name with String.from instead:
type Box[T] = { value: T }
let s = Reflect.shape(Reflect.erase(type(Box[int])))
let d = if s is Reflect.Record { s.decl } else { null }
Json.stringify(d) // panic: Json.stringify: a declaration has no JSON form
String.from(d) // "Box"See also
Reflect.shape, Reflect.Shape, Reflect.Type, Reflect.ErasedType, Reflect.erase
