Skip to main content

Type system

The TEN framework uses a type system to define and validate data types for message properties and extension configurations. This type system ensures data integrity across different programming languages and provides automatic type conversion when safe to do so.

The type system offers several key benefits:

  • Cross-language compatibility: Consistent type definitions work across C++, Go, and Python extensions
  • Automatic validation: Types defined in TEN schemas are validated at runtime
  • Safe conversions: Automatic type conversion when no data loss occurs
  • Clear contracts: Explicit type definitions make extension interfaces predictable

You can declare types for message properties and extension properties through TEN schemas in your manifest files, or let the framework infer types from initial values.

Basic Types

The TEN framework provides fundamental data types that map consistently across supported programming languages.

Numeric types

TypeDescriptionC++ typeGo typePython type
int8An 8-bit signed integerint8_tint8int
int16A 16-bit signed integerint16_tint16int
int32A 32-bit signed integerint32_tint32int
int64A 64-bit signed integerint64_tint64int
uint8An 8-bit unsigned integeruint8_tuint8int
uint16A 16-bit unsigned integeruint16_tuint16int
uint32A 32-bit unsigned integeruint32_tuint32int
uint64A 64-bit unsigned integeruint64_tuint64int
float32A single precision (32-bit) IEEE 754 floating-point numberfloatfloat32float
float64A double-precision (64-bit) IEEE 754 floating-point numberdoublefloat64float

Text and binary types

TypeDescriptionC++ TypeGo TypePython Type
stringA Unicode character sequencestd::string / char*stringstr
bufA sequence of 8-bit unsigned bytesuint8_t*[]bytebytearray / memoryview

Boolean and pointer types

TypeDescriptionC++ typeGo typePython type
boolA binary value, either true or falseboolboolbool
ptrA pointer to a memory addressvoid*unsafe.Pointer

Composite types

Composite types allow you to work with structured data and collections.

Arrays

Arrays contain collections of elements of the same type. They provide ordered storage for multiple values.

Objects

Objects represent complex key-value pairs where keys are always strings. They enable structured data with named properties.

TypeDescriptionUse cases
arrayA collection of elements of the same typeLists, sequences, collections of similar data
objectComplex key/value pairs with string keysConfiguration structures, nested properties

Working with composite types

You can access basic types directly using methods like get_property() and set_property().


_5
// Get property value
_5
int32_t value = cmd.get_property_`int32`("property_name");
_5
_5
// Set property value
_5
cmd.set_property("property_name", 100);

Composite types typically require serialization methods for access:


_7
type MyProp struct {
_7
Name string `json:"name"`
_7
}
_7
_7
var prop MyProp
_7
bytes, _ := json.Marshal(&prop)
_7
cmd.SetPropertyFromJSONBytes("property_name", bytes)

Type schemas and validation

The TEN framework determines property types through two methods: explicit schema definitions or automatic inference from initial values.

Schema-defined types

When you specify a TEN schema in your manifest file, the framework uses the schema to determine property types and validate values:


_12
{
_12
"api": {
_12
"property": {
_12
"timeout": {
_12
"type": "`int32`"
_12
},
_12
"enabled": {
_12
"type": "bool"
_12
}
_12
}
_12
}
_12
}

Schema-defined types provide strict validation and ensure type safety across extension boundaries.

Inferred types

When a TEN schema is not specified, the framework infers property types from initial value assignments:

  • Direct assignment: If you assign an int32_t value, the property becomes int32_t
  • JSON assignment: Types follow JSON processing rules; numbers default to int64 for integers, float64 for floating-point.

Type validation

The framework validates types when:

  • Loading property.json files
  • Calling methods like set_property_from_json()
  • Sending messages with typed properties

The following table summarizes the validation behavior:

Schema definedValidation behavior
YesStrict type checking against schema definition
NoType inferred from first assignment, flexible thereafter

Type conversion rules

The TEN framework supports automatic type conversion between compatible types, but only when the conversion preserves data integrity.

Safe conversions

Safe conversions move from lower to higher precision types and never lose data. The framework performs these conversions automatically:

Integer promotions

FromTo (Allowed)
int8int16 / int32 / int64
int16int32 / int64
int32int64
uint8uint16 / uint32 / uint64
uint16uint32 / uint64
uint32uint64

Floating-point promotions

FromTo (Allowed)
float32float64

Example of safe conversion:


_10
// Set property value.
_10
// The type of `property_name` in TEN Runtime is `int32`.
_10
cmd.set_property("property_name", 100);
_10
_10
// Get property value. Correct.
_10
int32_t value = cmd.get_property_int32("property_name");
_10
_10
// Get property value. Correct.
_10
// TEN Type System automatically converts the type to `int64`.
_10
int64_t value2 = cmd.get_property_int64("property_name");

Unsafe conversions

Unsafe conversions move from higher to lower precision types and may lose data. The framework checks for overflow and throws an error when data loss would occur. The TEN runtime performs unsafe conversions only when deserializing JSON documents into TEN properties that have defined TEN schemas:

  • Loading property.json files
    Integers are parsed as int64 by default and floating-point numbers as float64 by default. The framework applies unsafe conversion rules to match the target schema types.

  • Calling set_property_from_json() methods
    When passing serialized JSON strings, the framework performs unsafe conversion according to the defined schema requirements.

Unsafe conversion scenarios

In the TEN framework type system, unsafe conversion rules are as follows:

  • Converting int64 to lower precision int types
  • Converting int64 to any uint type
  • Converting float64 to float32

Example of unsafe conversion error:


_2
// Get property value. Incorrect use, an error is thrown.
_2
int16_t error_type = cmd.get_property_int16("property_name");

Unsafe conversions succeed only when the source value fits within the target type's range:

FromToValid value range
int64int8[-2^7, 2^7 - 1]
int64int16[-2^15, 2^15 - 1]
int64int32[-2^31, 2^31 - 1]
int64uint8[0, 2^7 - 1]
int64uint16[0, 2^15 - 1]
int64uint32[0, 2^31 - 1]
int64uint64[0, 2^63 - 1]
float64float32[-3.4028234663852886e+38, 3.4028234663852886e+38]