Reflect.Scalar
type Reflect.Scalar = null | void | bool | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | float32 | float64 | stringReflect.Scalar is what Reflect.scalar returns: a leaf value in its own kind.
Details
intandbyteare in the union asint64anduint8, the types they alias.Reflect.scalarreturns the leaf in its own kind, soistells the ten number types apart.
Basic examples
Read a leaf out of a box; the result is a Reflect.Scalar:
Reflect.scalar(Reflect.valueOf(type(int), 42)) // 42
Reflect.scalar(Reflect.valueOf(type(string), "ant")) // "ant"Pass a Reflect.Scalar to Reflect.makeScalar to box it at a type known at run time:
let leaf: Reflect.Scalar = 8080
Reflect.makeScalar(Reflect.erase(type(uint16)), leaf) // Reflect.Value(8080: uint16)Scope
A value of any leaf kind fits the type:
let leaves: Array[Reflect.Scalar] = [null, true, 3, 2.5, float32(0.5), byte(7), "x"]
leaves.length() // 7The leaf keeps its own kind, so is tests it exactly. A uint8 is not an int:
let s = Reflect.scalar(Reflect.valueOf(type(byte), byte(200)))
s is uint8 // true
s is int // falseThe void value comes from a function that returns nothing:
func reset() {}
let s = Reflect.scalar(Reflect.valueOf(type(void), reset()))
s is void // truenull is a member, so a parse that may fail fits the type as it is:
let parsed: Reflect.Scalar = "4x2".toInt()
parsed // nullApplications
An encoder renders any leaf with String.from, and tests only the kinds it writes differently:
func encode(v: Reflect.Scalar): string {
if v is string { return "\"${v}\"" }
if v == null { return "null" }
String.from(v)
}
println(encode(Reflect.scalar(Reflect.valueOf(type(float32), float32(0.5)))))
println(encode(Reflect.scalar(Reflect.valueOf(type(int64), 9))))
println(encode(Reflect.scalar(Reflect.valueOf(type(string), "hi"))))
println(encode(Reflect.scalar(Reflect.valueOf(type(int | null), null))))0.5
9
"hi"
nullA decoder parses text into the kind its slot needs, as a Reflect.Scalar, and leaves the range and the exact type to Reflect.makeScalar. A parse that fails gives null, which Reflect.makeScalar rejects:
func leaf(t: Reflect.ErasedType, text: string): Reflect.Value | null {
let s = Reflect.shape(t)
let v: Reflect.Scalar = if s is Reflect.Int {
text.toInt()
} else if s is Reflect.Float {
text.toFloat()
} else if s is Reflect.Bool {
if text == "true" { true } else if text == "false" { false } else { null }
} else {
text
}
Reflect.makeScalar(t, v)
}
let port = Reflect.erase(type(uint16))
println(leaf(port, "8080"))
println(leaf(port, "-1"))
println(leaf(port, "http"))
println(leaf(Reflect.erase(type(bool)), "true"))Reflect.Value(8080: uint16)
null
null
Reflect.Value(true: bool)Properties and relations
Boxing a leaf at its box's type gives the box back:
let d = Reflect.valueOf(type(int16), int16(-3))
let s: Reflect.Scalar = Reflect.scalar(d)
assert(Reflect.makeScalar(Reflect.type(d), s) == d)Possible issues
A named primitive value is not a Reflect.Scalar. Use its base value, with a conversion such as string(id):
type UserId = string
let id = UserId("u-1")
let leaf: Reflect.Scalar = id // error: cannot use UserId where Reflect.Scalar is expectedA Reflect.Scalar is not a number, so arithmetic on it is a compile error. Narrow it with is first, or take the value out with Reflect.to when you know its type:
let s = Reflect.scalar(Reflect.valueOf(type(int), 2))
let n = s + 1 // error: + needs both operands of one type, got Reflect.Scalar and intlet d = Reflect.valueOf(type(int), 2)
let s = Reflect.scalar(d)
if s is int { s + 1 } else { 0 } // 3
Reflect.to(d, type(int)) + 1 // 3A box of a named primitive type holds no Reflect.Scalar, so Reflect.scalar panics on it. Take the base value out with Reflect.payload first:
type UserId = string
let d = Reflect.valueOf(type(UserId), UserId("u-1"))
Reflect.scalar(d) // panic: Reflect.scalar needs a value of a scalar type, got one of type UserId
Reflect.scalar(Reflect.payload(d)) // "u-1"See also
Reflect.scalar, Reflect.makeScalar, Reflect.Value, Reflect.Shape, Reflect.payload
