Declaring Data Objects and Types
Built-in Types
Constants (const)
A constant object of a data type may be declared using the const
keyword.
Syntax
const NAME : TYPE = VALUE;
Example
Declare a constant object of the float64
data type named pi
:
flex
const pi : float64 = 3.14159265359;
Declare a constant object of an array of the uint8
data type named uuid
:
flex
const uuid : uint8[] = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0];
Declare a constant object of a user-defined struct space3D
data type named coord
:
flex
const coord : space3D = space3D {x = 1.2; y = -2.0; z = 6.0;};
Structured Types (struct)
A structured type object may be declared using the struct
keyword.
Zero or more fields may be declared within a struct, and each field must have a data type declared after its name. Properties may be declared after field declarations.
Syntax
struct NAME { FIELDS ; PROPERTIES }
Example
Declare a struct
with fields of the float64
type named space3D
:
flex
struct space3D {x : float64;y : float64;z : float64;}
Declare a struct
with fields of Optional
types named Result
:
flex
struct Result {num : Optional<int32>;str : Optional<string>;}
Struct Properties
Structs may include field assertions after field declarations. Field assertions are declared using the assert
keyword.
Example
Declare a struct
named GreaterThan100
with a field named count
that must be greater than 100
:
flex
struct GreaterThan100 {count : int32;assert count > 100;}
Extending Structs
A struct can extend an existing one using the extends
keyword. The new struct will contain the fields from the existing struct plus the new ones declared.
To make a struct extensible, it must be declared using the extensible struct
keyword.
Example
Declare an extensible struct
named Base
, then declare a struct
named BaseExtended
that extends it:
flex
extensible struct Base {ID : uint8;Name : string;}struct BaseExtended extends Base {Time : float64;}
BaseExtended
will contain the fields ID
, Name
, and Time
.
Enumerated Types (enum)
An enumerated type object may be declared using the enum
keyword.
Its data type must be declared after its name, and all the values within it must be of that data type.
Syntax
enum NAME TYPE { FIELD VALUE PAIRS }
Example
Declare an enum
named SimulationStatusType
with values of the int32
type:
flex
enum SimulationStatusType int32 {Stopped = 0;Running = 1;Paused = 2;Reset = 3;}
Variant Types (variant)
A variant type object may be declared using the variant
keyword.
Variants are useful when handling different result types. For example, if you need the result of a function to either be of type string
or float32
, then a variant can facilitate that.
Syntax
variant NAME { FIELDS }
Example
Declare a variant
named Result
with two possible types, either string
or float32
:
flex
variant Result {Error(string);Value(float32);}
Message Types (message)
Messages are a special type of object that you can use in Tangram ProTM when designing component software interfaces.
They may be used for the parameters or return value of transform functions. A message type object may either be a struct
or a variant
.
Message Structs
A message struct type object may be declared using the message struct
keyword. They are similar to Structured Types.
Syntax
message struct NAME { FIELDS ; PROPERTIES }
Example
Declare a message struct
named Route
which utilizes a struct
named Location
:
flex
struct Location {Latitude : float64;Longitude : float64;Altitude : float32;}message struct Route {RouteID : int64;Start : Location;Stop : Location;}
Message Struct Properties
Message structs may include field assertions after field declarations. See Struct Properties.
Extending Message Structs
Message structs can be extended. See Extending Structs.
Message Variants
A message variant type object may be declared using the message variant
keyword. They are similar to Variant Types.
Syntax
message variant NAME { FIELDS }
Example
Declare a message variant
named Result
:
flex
message variant Result {Error(string);Value(float32);}
New Types (newtype)
A new type object may be declared using the newtype
keyword.
New types must have one field declared within it, along with its data type. Properties may be declared after the field declaration.
New types may be used for the parameters or return value of transform functions.
Syntax
newtype NAME {FIELD ; PROPERTIES}
Example
Declare two newtype
objects named MPH
and KMPH
which both utilize a struct field Velocity.speed
:
flex
struct Velocity {speed : float64;angle : float64;}newtype MPH {value : Velocity.speed;}newtype KMPH {value : Velocity.speed;}
New Type Properties
Similar to structs, new types may include field assertions after the field declaration. See Struct Properties.
Accessing Fields
Object fields are accessed using the dot syntax.
Syntax
OBJECT.FIELD
Example
Given a struct
named space3D
, and a const
of it named coord
:
flex
struct space3D {x : float64;y : float64;z : float64;}const coord : space3D = space3D {x = 1.2; y = -2.0; z = 6.0;};
Access the field x
:
flex
coord.x