Skip to main content
Tolk supports type aliases, similar to TypeScript and Rust. An alias creates a new name for an existing type and remains fully interchangeable with it.

Aliases are interchangeable with underlying types

UserId and int32 from the above are fully equivalent:
  • id + 1 is okay, will be int
  • someF(id) is okay if someF accepts int32 or int
  • methods for int32 can be called having UserId and vice versa (and for int also, because int32 is assignable to int)
  • a union UserId | int32 makes no sense, it is simply int32
To obtain a “strict alias”, which defines a distinct type, use a struct with one field:
Such a struct has no overhead over int32, but it becomes a distinct type with its own methods and semantics.

Two equal aliases are considered different

If two aliases share the same underlying type,
Then they are not assignable to each other. It allows them to have identical methods:
This reminds intN types: int32 is assignable to int and back, int64 also, but assigning int32 to int64 is something strange. Assignment can be done with explicit casting: b as AssetsDict, see operator as.

Type aliases can be generic

Read about generic structs and aliases.

Stack layout and serialization

An alias is identical to its underlying type. Serialization can be overloaded with custom serializers. This is useful in tricky cases where binary encoding cannot be expressed using existing types.
For details, follow TVM representation and Serialization.