Skip to content

Data Types

NanoPack supports the following data types out of the box.

Number types

NanoPack supports the following data types:

  • 8-bit signed/unsigned integers: int8/uiut8
  • 32-bit signed/unsigned integers: int32/uint32
  • 64-bit signed/unsigned integers: int64/uint64
  • Double-precision floating-points: (Doubles): double

Strings

NanoPack also supports UTF8-encoded strings. Use string as the type keyword.

Booleans

Use bool to denote a boolean type.

Arrays

NanoPack supports arrays of any NanoPack types. Simply add a pair of square brackets [] to the end of a type to make it an array of that type. For example, a string array is declared as string[].

NanoPack also supports nested arrays. string[][], for example, declares an array of string arrays.

Maps

NanoPack supports maps as well. Only strings and number types can be used as map keys, but map values can be of any type.

To declare a map type, use the following syntax:

<key-type:value-type>

For example, to declare a field my_field that stores map of string to 32-bit integers:

my_field: <string:int32>

Optional

A type can be made optional by adding a question mark (?) at the end of a type:

# an optional array of string
field_1: string[]?
# an array of optional strings
field_2: string?[]

Except keys of maps, any type can be made optional.

Messages

NanoPack supports using other NanoPack messages as types. A message field can store a message of another message type, or even of its own type (recursive types).

To use another message as a type, simply use its name as the type name. No import statement is required to make it available.

Any message

If you want to store any NanoPack message, use message as the type.

Enums

A field can also store any user-defined enums. Use the name of the enum as the type. No import statement is required.

Any

A field can be of any arbitrary type by specifying the type as any. How the any type is handled depends on the programming language. Refer to the corresponding language guide for more details.

Combining Different Data Types.

Here is an example of combining different types:

CrazyMessage:
# Array of map of string to int32
field_1: <string:int32>[]
# Map of int32 to string arrays
field_2: <int32:string[]>
# Array of map of strings to string arrays
field_3: <string:string[]>[]
# Map of strings to map of int32 to int8
field_4: <string:<int32:int8>>