Tengo Runtime Types

March 4, 2020 ยท View on GitHub

  • Int: signed 64bit integer
  • String: string
  • Float: 64bit floating point
  • Bool: boolean
  • Char: character (rune in Go)
  • Bytes: byte array ([]byte in Go)
  • Array: objects array ([]Object in Go)
  • ImmutableArray: immutable object array ([]Object in Go)
  • Map: objects map with string keys (map[string]Object in Go)
  • ImmutableMap: immutable object map with string keys (map[string]Object in Go)
  • Time: time (time.Time in Go)
  • Error: an error with underlying Object value of any type
  • Undefined: undefined

Type Conversion/Coercion Table

src\dstIntStringFloatBoolCharBytesArrayMapTimeErrorUndefined
Int-strconvfloat64(v)!IsFalsy()rune(v)XXXtime.Unix()XX
Stringstrconv-strconv!IsFalsy()X[]byte(s)XXXXX
Floatint64(f)strconv-!IsFalsy()XXXXXXX
Bool1 / 0"true" / "false"X-XXXXXXX
Charint64(c)string(c)X!IsFalsy()-XXXXXX
BytesXstring(y)X!IsFalsy()X-XXXXX
ArrayX"[...]"X!IsFalsy()XX-XXXX
MapX"{...}"X!IsFalsy()XXX-XXX
TimeXString()X!IsFalsy()XXXX-XX
ErrorX"error: ..."XfalseXXXXX-X
UndefinedXXXfalseXXXXXX-

* X: No conversion; Typed value functions for Variable will return zero values.
* strconv: converted using Go's conversion functions from strconv package.
* IsFalsy(): use Object.IsFalsy() function
* String(): use Object.String() function * time.Unix(): use time.Unix(v, 0) to convert to Time

Object.IsFalsy()

Object.IsFalsy() interface method is used to determine if a given value should evaluate to false (e.g. for condition expression of if statement).

  • Int: n == 0
  • String: len(s) == 0
  • Float: isNaN(f)
  • Bool: !b
  • Char: c == 0
  • Bytes: len(bytes) == 0
  • Array: len(arr) == 0
  • Map: len(map) == 0
  • Time: Time.IsZero()
  • Error: true (Error is always falsy)
  • Undefined: true (Undefined is always falsy)

Type Conversion Builtin Functions

  • string(x): tries to convert x into string; returns undefined if failed
  • int(x): tries to convert x into int; returns undefined if failed
  • bool(x): tries to convert x into bool; returns undefined if failed
  • float(x): tries to convert x into float; returns undefined if failed
  • char(x): tries to convert x into char; returns undefined if failed
  • bytes(x): tries to convert x into bytes; returns undefined if failed
    • bytes(N): as a special case this will create a Bytes variable with the given size N (only if N is int)
  • time(x): tries to convert x into time; returns undefined if failed
  • See Builtins for the full list of builtin functions.

Type Checking Builtin Functions

  • is_string(x): returns true if x is string; false otherwise
  • is_int(x): returns true if x is int; false otherwise
  • is_bool(x): returns true if x is bool; false otherwise
  • is_float(x): returns true if x is float; false otherwise
  • is_char(x): returns true if x is char; false otherwise
  • is_bytes(x): returns true if x is bytes; false otherwise
  • is_array(x): return true if x is array; false otherwise
  • is_immutable_array(x): return true if x is immutable array; false otherwise
  • is_map(x): return true if x is map; false otherwise
  • is_immutable_map(x): return true if x is immutable map; false otherwise
  • is_time(x): return true if x is time; false otherwise
  • is_error(x): returns true if x is error; false otherwise
  • is_undefined(x): returns true if x is undefined; false otherwise
  • See Builtins for the full list of builtin functions.