Data Types
NanoPack supports the following data types out of the box.
Number types
NanoPack supports the following data types:
- 8-bit integers:
int8
- 32-bit integers:
int32
- 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 field.
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:
For example, to declare a field my_field
that stores map of string to 32-bit integers:
Optional
A type can be made optional by adding a question mark (?
) at the end of a type:
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.
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.
Type nesting
Since arrays and maps can store values of any type, you can go ham with the type!