Reflect.scalar
func Reflect.scalar(d: Reflect.Value): Reflect.ScalarReflect.scalar returns the value in d in its own kind. It panics if d's type is not null, void, bool, a number, or string.
Details
- The result has the value's own kind, not a common one: a box of type
uint8gives auint8, and a box of typefloat32gives afloat32. Test the kind withisbefore using it as a number. - A box made at a union holds the member its value belongs to, so a box of type
int | nullgives theintor thenullinside it. - It panics on any other type: a named primitive type such as
type Celsius = float64, a record, a container, or an opaque type such asBytes. Unwrap a named primitive withReflect.payloadfirst. intandbyteare aliases ofint64anduint8, so a box of typeintgives a value that is bothintandint64.
Basic examples
Take a leaf out of a box:
Reflect.scalar(Reflect.valueOf(type(int), 42)) // 42
Reflect.scalar(Reflect.valueOf(type(string), "ant")) // "ant"The value keeps its own number type:
let v = Reflect.scalar(Reflect.valueOf(type(uint8), uint8(7)))
v is uint8 // true
v is int // falseScope
Every scalar kind
Booleans, sized integers, floats, null, and void all come out as themselves:
func reset() {}
Reflect.scalar(Reflect.valueOf(type(bool), true)) // true
Reflect.scalar(Reflect.valueOf(type(float32), float32(1.5))) // 1.5
Reflect.scalar(Reflect.valueOf(type(float64), 2.5)) // 2.5
Reflect.scalar(Reflect.valueOf(type(int64), int64(-3))) is int64 // true
Reflect.scalar(Reflect.valueOf(type(int), 1)) is int64 // true
Reflect.scalar(Reflect.valueOf(type(null), null)) // null
Reflect.scalar(Reflect.valueOf(type(void), reset())) is void // trueBoxes made at a union
A box made at int | null holds whichever member the value is:
let some: int | null = 3
let none: int | null = null
Reflect.scalar(Reflect.valueOf(type(int | null), some)) // 3
Reflect.scalar(Reflect.valueOf(type(int | null), none)) // nullThrough . and pipes
Reflect.scalar takes the box first, so it ends a chain of readers:
type Point = { x: int, y: int }
let d = Reflect.valueOf(type(Point), Point{ x: 1, y: 2 })
d.field("y").scalar() // 2
d | Reflect.field("x") | Reflect.scalar() // 1Named primitive types
A named primitive type is not a scalar type. Read its payload, which has the base type:
type Celsius = float64
let d = Reflect.valueOf(type(Celsius), Celsius(21.5))
d.payload().scalar() // 21.5Applications
Write any record as a CSV row. Each field's leaf goes through String.from, whatever its number type:
type Reading = { sensor: string, value: float32, count: uint16, ok: bool }
func csvRow(d: Reflect.Value): string {
let s = Reflect.shape(Reflect.type(d))
if !(s is Reflect.Record) {
return String.from(d.scalar())
}
s.fields | Array.map({ String.from(d.field(x.name).scalar()) }) | String.join(",")
}
let r = Reading{ sensor: "a1", value: float32(21.5), count: uint16(3), ok: true }
println(csvRow(Reflect.valueOf(type(Reading), r)))a1,21.5,3,trueRender a leaf as JSON text, testing only the kinds that need their own form:
func jsonLeaf(d: Reflect.Value): string {
let v = d.scalar()
if v is string { return Json.stringify(v) }
if v == null || v is void { return "null" }
String.from(v)
}
func reset() {}
let leaves = [
Reflect.valueOf(type(string), "say \"hi\""),
Reflect.valueOf(type(uint64), Math.maxUint64),
Reflect.valueOf(type(float32), float32(0.1)),
Reflect.valueOf(type(bool), false),
Reflect.valueOf(type(void), reset()),
]
for d in leaves {
println(jsonLeaf(d))
}"say \"hi\""
18446744073709551615
0.1
false
nullProperties and relations
For a scalar type, Reflect.scalar gives the same value as Reflect.to at that type:
let d = Reflect.valueOf(type(int16), int16(-5))
assert(Reflect.scalar(d) == Reflect.to(d, type(int16)))Reflect.makeScalar is its inverse: boxing the leaf at the box's type gives an equal box:
let d = Reflect.valueOf(type(float64), 2.5)
assert(Reflect.makeScalar(Reflect.type(d), d.scalar()) == d)Possible issues
The result is a Reflect.Scalar, a union, so it is not a number until you test its kind:
let d = Reflect.valueOf(type(int), 1)
let n = Reflect.scalar(d) + 1
// error: + needs both operands of one type, got Reflect.Scalar and intlet d = Reflect.valueOf(type(int), 1)
let v = d.scalar()
if v is int { v + 1 } else { 0 } // 2A uint8 is not an int, so a test for int alone misses the other number types:
let v = Reflect.valueOf(type(uint8), uint8(200)).scalar()
if v is int { "int" } else { "something else" } // "something else"A named primitive type panics, even when its base is a scalar:
type Celsius = float64
Reflect.scalar(Reflect.valueOf(type(Celsius), Celsius(21.5)))
// panic: Reflect.scalar needs a value of a scalar type, got one of type CelsiusSo do records and containers. Test the shape first when the type is not known:
Reflect.scalar(Reflect.valueOf(type(Array[int]), [1]))
// panic: Reflect.scalar needs a value of a scalar type, got one of type Array[int]let d = Reflect.valueOf(type(Array[int]), [1])
let s = Reflect.shape(Reflect.type(d))
let leaf = s is Reflect.Bool || s is Reflect.Int || s is Reflect.Float || s is Reflect.String
if leaf { String.from(d.scalar()) } else { "not a leaf" } // "not a leaf"See also
Reflect.makeScalar · Reflect.to · Reflect.payload · Reflect.field · Reflect.items · Reflect.shape · Reflect.valueOf · Reflect.Scalar
