Reflect.nullable
func Reflect.nullable[T](t: Reflect.Type[T]): Reflect.Type[T | null]Reflect.nullable returns type(T | null) for a T held as a type value. Unions flatten, so a T that already includes null gives the same type.
Details
- Adding
nullto a type that already admits it changes nothing, sot.nullable()is a safe way to make anyToptional.
Basic examples
Reflect.nullable(type(int)) // int | null
Reflect.nullable(type(int)) == type(int | null) // trueInside a generic function, Reflect.nullable builds the type that type(T | null) cannot name:
func optional[T](t: Reflect.Type[T]): Reflect.Type[T | null] {
Reflect.nullable(t)
}
optional(type(string)) // string | null
optional(type(string)) == type(string | null) // trueScope
The argument may be any type a type expression can hold, such as a record, a container, a prelude type, or a function type:
type Point = { x: int, y: int }
Reflect.nullable(type(Point)) // Point | null
Reflect.nullable(type(Array[int])) // Array[int] | null
Reflect.nullable(type(Result[int, Error])) // Result[int, Error] | null
Reflect.nullable(type(func(int): string)) // (func(int): string) | nullA union gains null as one more member:
Reflect.nullable(type(int | string)) // int | string | nullA type that already includes null stays the same type, whether it is written out, named by an alias, or null itself:
type MaybeInt = int | null
Reflect.nullable(type(int | null)) // int | null
Reflect.nullable(type(null)) // null
Reflect.nullable(type(MaybeInt)) == type(MaybeInt) // true
Reflect.nullable(type(Json.Value)) == type(Json.Value) // trueCall it through . on a type value, or as a pipe stage:
type(bool).nullable() // bool | null
type(bool) | Reflect.nullable() // bool | nullIt composes with the other builders. The order decides which part may be null:
Reflect.arrayOf(Reflect.nullable(type(int))) // Array[int | null]
Reflect.nullable(Reflect.arrayOf(type(int))) // Array[int] | null
Reflect.mapOf(type(string), Reflect.nullable(type(float64))) // Map[string, float64 | null]Applications
Take a boxed value out at an optional type, for a T known only to the caller, as a decoder does for a field that may be missing. Reflect.to accepts both a T and a null:
func unbox[T](t: Reflect.Type[T], d: Reflect.Value): T | null {
Reflect.to(d, t.nullable())
}
println(unbox(type(int), Reflect.valueOf(type(int), 42)))
println(unbox(type(int), Reflect.valueOf(type(null), null)))42
nullAsk whether a type already admits null: adding null leaves such a type unchanged. The static types Reflect.Type[T | null] and Reflect.Type[T] differ, so compare the two erased:
func acceptsNull[T](t: Reflect.Type[T]): bool {
Reflect.erase(t.nullable()) == Reflect.erase(t)
}
acceptsNull(type(int)) // false
acceptsNull(type(string | null)) // true
acceptsNull(type(Json.Value)) // trueLook up a codec by type value. A table keyed by type(int | null) finds the entry for a type built with Reflect.nullable, since the two are == and hash alike:
let names: Map[Reflect.ErasedType, string] = Map{}
names[Reflect.erase(type(int | null))] = "optional int"
func nameOf[T](t: Reflect.Type[T]): string {
names.get(Reflect.erase(t.nullable())) ?? "unknown"
}
nameOf(type(int)) // "optional int"
nameOf(type(int | null)) // "optional int"
nameOf(type(string)) // "unknown"Properties and relations
The shape of the result is a union of the argument and null:
let t = type(int).nullable()
let s = Reflect.shape(Reflect.erase(t))
assert(s is Reflect.Union && s.members == [Reflect.erase(type(int)), Reflect.erase(type(null))])The argument is assignable to the result, and so is null:
type Point = { x: int, y: int }
let t = Reflect.erase(type(Point))
let n = Reflect.erase(type(Point).nullable())
assert(Reflect.assignable(t, n))
assert(Reflect.assignable(Reflect.erase(type(null)), n))The call is idempotent:
let once = type(string).nullable()
assert(once.nullable() == once)Possible issues
A type expression in a generic function may not mention the function's type parameter. The compile error points to the builders:
func optional[T](t: Reflect.Type[T]): Reflect.Type[T | null] {
type(T | null)
// error: type(...) cannot mention T, a type parameter of the enclosing function or type, since nothing is bound to it at run time; take a Reflect.Type[T] parameter and build the type from it with Reflect.arrayOf, Reflect.setOf, Reflect.mapOf, or Reflect.nullable
}An erased type has no static type to build from, so passing one is a compile error. Keep the typed Reflect.Type[T] and erase the result instead:
let e = Reflect.erase(type(int))
let maybe = Reflect.nullable(e) // error: cannot use Reflect.ErasedType where Reflect.Type[T] is expectedSee also
Reflect.arrayOf · Reflect.setOf · Reflect.mapOf · Reflect.erase · Reflect.shape · Reflect.assignable · Reflect.to · Reflect
