is
Function
is — test a value’s type
Synopsis
is(t: type) -> bool
is(val: any, t: type) -> bool
Description
The is function returns true if the argument val
is of type t
. If val
is omitted, it defaults to this
. The is function is shorthand for typeof(val)==t
.
Examples
Test simple types:
yield {yes:is(<float64>),no:is(<int64>)}
1.
Loading...
echo '1.' \
| super -z -c 'yield {yes:is(<float64>),no:is(<int64>)}' -
Test for a given input’s record type or “shape”:
yield is(<{s:string}>)
{s:"hello"}
Loading...
echo '{s:"hello"}' \
| super -z -c 'yield is(<{s:string}>)' -
If you test a named type with its underlying type, the types are different, but if you use the type name or typeof and under functions, there is a match:
yield is(<{s:string}>)
{s:"hello"}(=foo)
Loading...
echo '{s:"hello"}(=foo)' \
| super -z -c 'yield is(<{s:string}>)' -
yield is(<foo>)
{s:"hello"}(=foo)
Loading...
echo '{s:"hello"}(=foo)' \
| super -z -c 'yield is(<foo>)' -
To test the underlying type, just use ==
:
yield under(typeof(this))==<{s:string}>
{s:"hello"}(=foo)
Loading...
echo '{s:"hello"}(=foo)' \
| super -z -c 'yield under(typeof(this))==<{s:string}>' -