Reflect.assignable
func Reflect.assignable(from: Reflect.ErasedType, to: Reflect.ErasedType): boolReflect.assignable reports whether a value of type from may be used where to is expected, on two closed types. It is the relation the readers, the builders, and Reflect.to use, exposed so that a library can test before calling a function that would panic.
Details
- The first argument is the type you have and the second is the slot it goes into. Swapping them asks the opposite question.
- It applies the type checker's own rules, so it is
trueexactly where the checker accepts a variable of typefromwheretois expected.
Basic examples
An int fits where int | null is expected, but not the other way around:
let n = Reflect.erase(type(int))
let maybe = Reflect.erase(type(int | null))
Reflect.assignable(n, maybe) // true
Reflect.assignable(maybe, n) // falseEvery type is assignable to itself, and int is another name for int64:
Reflect.assignable(Reflect.erase(type(string)), Reflect.erase(type(string))) // true
Reflect.assignable(Reflect.erase(type(int)), Reflect.erase(type(int64))) // trueScope
Primitives
Numbers never widen, and a named primitive type is not its base:
type UserId = int
let n = Reflect.erase(type(int))
Reflect.assignable(Reflect.erase(type(int8)), n) // false
Reflect.assignable(Reflect.erase(type(UserId)), n) // falsenever fits everywhere. null fits only a type that includes it, and void only void:
let text = Reflect.erase(type(string))
let empty = Reflect.erase(type(null))
Reflect.assignable(Reflect.erase(type(never)), text) // true
Reflect.assignable(empty, text) // false
Reflect.assignable(empty, Reflect.erase(type(string | null))) // true
Reflect.assignable(empty, Reflect.erase(type(void))) // falseUnions
A member fits its union. A union fits a type only when every one of its members does:
type Circle = { radius: float64 }
type Rect = { width: float64, height: float64 }
type Shape = Circle | Rect
let shape = Reflect.erase(type(Shape))
let maybeCircle = Reflect.erase(type(Circle | null))
Reflect.assignable(Reflect.erase(type(Circle)), shape) // true
Reflect.assignable(maybeCircle, shape) // false
Reflect.assignable(maybeCircle, Reflect.erase(type(Shape | null))) // trueRecords
A named record type accepts only its own tag. An anonymous record type accepts any record with at least its fields, each with exactly that field's type:
type User = { name: string, age: int }
type Pet = { name: string }
let user = Reflect.erase(type(User))
Reflect.assignable(user, Reflect.erase(type({ name: string }))) // true
Reflect.assignable(user, Reflect.erase(type({ name: string | null }))) // false
Reflect.assignable(user, Reflect.erase(type(Pet))) // falseGeneric types
Type arguments must be identical, even when one would fit the other:
type Circle = { radius: float64 }
type Rect = { width: float64, height: float64 }
type Shape = Circle | Rect
let circles = Reflect.erase(type(Array[Circle]))
Reflect.assignable(circles, Reflect.erase(type(Array[Shape]))) // false
Reflect.assignable(circles, Reflect.erase(type(Array[Circle]))) // true
Reflect.assignable(
Reflect.erase(type(Result[int, Error])),
Reflect.erase(type(Result[int | null, Error])),
) // falseFunction types
A function that takes a wider parameter fits a slot that passes a narrower one, and not the reverse:
type Circle = { radius: float64 }
type Rect = { width: float64, height: float64 }
type Shape = Circle | Rect
let anyShape = Reflect.erase(type(func(Shape): float64))
let onlyCircles = Reflect.erase(type(func(Circle): float64))
Reflect.assignable(anyShape, onlyCircles) // true
Reflect.assignable(onlyCircles, anyShape) // falseRecursive aliases
A recursive alias is related by its structure, at any depth:
type F = func(): F | null
type G = func(): G | int | null
Reflect.assignable(Reflect.erase(type(F)), Reflect.erase(type(G))) // true
Reflect.assignable(Reflect.erase(type(G)), Reflect.erase(type(F))) // falseTypes from values and shapes
The type of a box comes from Reflect.type, and a child type comes from Reflect.shape. Both are erased already:
let box = Reflect.valueOf(type(int | null), 42)
let s = Reflect.shape(Reflect.erase(type(Array[int | null])))
Reflect.type(box) // int
s is Reflect.Array && Reflect.assignable(Reflect.type(box), s.element) // trueApplications
Unbox without a panic
Reflect.to panics when the box does not fit. Test first to get null instead:
func cast[T](box: Reflect.Value, t: Reflect.Type[T]): T | null {
if !Reflect.assignable(Reflect.type(box), Reflect.erase(t)) { return null }
Reflect.to(box, t)
}
let box = Reflect.valueOf(type(string), "ant")
println(cast(box, type(string | null)))
println(cast(box, type(int)))ant
nullKeep only the items that fit
Reflect.makeArray panics on an item that does not fit the element type. Filter the items first:
type Circle = { radius: float64 }
type Rect = { width: float64, height: float64 }
type Shape = Circle | Rect
let t = Reflect.erase(type(Array[Shape]))
let s = Reflect.shape(t)
if !(s is Reflect.Array) { return }
let items = [
Reflect.valueOf(type(Circle), Circle{ radius: 1.0 }),
Reflect.valueOf(type(string), "not a shape"),
Reflect.valueOf(type(Rect), Rect{ width: 2.0, height: 3.0 }),
]
let shapes = items.filter({ Reflect.assignable(Reflect.type(x), s.element) })
println(Reflect.makeArray(t, shapes))Reflect.Value([Circle{ radius: 1.0 }, Rect{ width: 2.0, height: 3.0 }]: Array[Shape])Pick a handler by type
Look up the first entry of a table whose type accepts the value:
type Handler = { accepts: Reflect.ErasedType, name: string }
let handlers = [
Handler{ accepts: Reflect.erase(type(int | float64)), name: "number" },
Handler{ accepts: Reflect.erase(type(string)), name: "text" },
Handler{ accepts: Reflect.erase(type(null)), name: "empty" },
]
func handlerFor(box: Reflect.Value): string {
let t = Reflect.type(box)
handlers.find({ Reflect.assignable(t, x.accepts) })?.name ?? "unknown"
}
println(handlerFor(Reflect.valueOf(type(float64), 2.5)))
println(handlerFor(Reflect.valueOf(type(string | null), null)))
println(handlerFor(Reflect.valueOf(type(bool), true)))number
empty
unknownProperties and relations
Reflect.to succeeds exactly when Reflect.assignable holds for the box's type:
let box = Reflect.valueOf(type(int), 7)
assert(Reflect.assignable(Reflect.type(box), Reflect.erase(type(int | string))))
let v: int | string = Reflect.to(box, type(int | string))
assert(v == 7)Every type fits itself, never fits every type, and a type fits its nullable form:
let types = [
Reflect.erase(type(int)),
Reflect.erase(type(Array[string])),
Reflect.erase(type(func(int): bool)),
Reflect.erase(type({ id: int })),
]
let bottom = Reflect.erase(type(never))
for t in types {
assert(Reflect.assignable(t, t))
assert(Reflect.assignable(bottom, t))
}
let n = type(Map[string, int])
assert(Reflect.assignable(Reflect.erase(n), Reflect.erase(Reflect.nullable(n))))Possible issues
A Reflect.Type[T] is not an erased type. Passing type(T) directly is a compile error:
Reflect.assignable(type(int), Reflect.erase(type(int | null)))
// error: cannot use Reflect.Type[int] where Reflect.ErasedType is expectedtype(T) cannot reach an interface, so Reflect.assignable cannot test whether a type satisfies one:
interface Show {
show(x: Self): string
}
let t = Reflect.erase(type(Show))
// error: type(...) cannot reach an interface: Show is one, and an interface has no run-time identityA box holding a union value has the type of the member the value belongs to, so the test is on that member, not on the union the box was made with:
let box = Reflect.valueOf(type(int | string), 1)
Reflect.assignable(Reflect.type(box), Reflect.erase(type(int))) // trueSee also
Reflect.to, Reflect.type, Reflect.shape, Reflect.erase, Reflect.makeArray, Reflect.makeRecord, Reflect.same
