Function
cast — convert a value to a different type
Synopsis
cast(val: any, target: type) -> any
cast(val: any, name: string) -> any
Description
The cast function implements a cast where the target
of the cast is a type value instead of a type.
In the first form,
the function converts val to the type indicated by target in accordance
with the semantics of the expression cast.
In the second form, the target type is a named type
whose name is the name parameter and whose type is the type of val.
When a cast is successful, the return value of cast always has the target type.
If errors are encountered, then some or all of the resulting value will be embedded with structured errors and the result does not have the target type.
Examples
Cast primitives to type ip
# spq
cast(this, <ip>)
# input
"10.0.0.1"
1
"foo"
# expected output
10.0.0.1
error({message:"cannot cast to ip",on:1})
error({message:"cannot cast to ip",on:"foo"})
Cast a record to a different record type
# spq
cast(this, <{b:string}>)
# input
{a:1,b:2}
{a:3}
{b:4}
# expected output
{b:"2"}
{b:null::string}
{b:"4"}
Create a named type and cast value to the new type
# spq
cast(this, "foo")
# input
{a:1,b:2}
{a:3,b:4}
# expected output
{a:1,b:2}::=foo
{a:3,b:4}::=foo
Derive type names from the properties of data
# spq
values cast(this, has(x) ? "point" : "radius")
# input
{x:1,y:2}
{r:3}
{x:4,y:5}
# expected output
{x:1,y:2}::=point
{r:3}::=radius
{x:4,y:5}::=point
Cast using a computed type value
# spq
values cast(val, type)
# input
{val:"123",type:<int64>}
{val:"123",type:<float64>}
{val:["true","false"],type:<[bool]>}
# expected output
123
123.
[true,false]