Type system overview
Every variable, item, and value in Tact programs has a type. They can be:
- One of the primitive types
- or composite types
Additionally, many of those types can be made nullable.
Primitive types
Tact supports a number of primitive data types that are tailored for smart contract use:
Int
— all numbers in Tact are -bit signed integers, but smaller representations can be used to reduce storage costs.Bool
— classical boolean withtrue
andfalse
values.Address
— standard smart contract address in TON Blockchain.Cell
,Builder
,Slice
— low-level primitives of TVM.String
— immutable text strings.StringBuilder
— helper type that allows you to concatenate strings in a gas-efficient way.
Booleans
The primitive type Bool
is the classical boolean type, which can hold only the two values: true
and false
. It’s convenient for boolean and logical operations, as well as for storing flags.
There are no implicit type conversions in Tact, so addition (+
) of two boolean values isn’t possible. Hovewer, many comparison operators are available, such as:
&&
for logical AND,||
for logical OR,!
for logical inversion,==
and!=
for checking equality,- and
!!
for non-null assertion.
Persisting bools to state is very space-efficient, as they only take 1-bit. Storing 1000 bools in state costs about TON per year.
Composite types
Using individual means of storage often becomes cumbersome, so there are ways to combine multiple primitive types together to create composite types:
- Maps — associations of keys with values.
- Structs and Messages — data structures with typed fields.
- Optionals —
null
values for variables or fields of Structs and Messages.
In addition to the composite types above, Tact provides a special type constructor bounced<T>
, which can only be specified in bounced message receivers.
Note, while contracts and traits are also considered a part of the Tacts type system, one can’t pass them around like Structs and Messages. Instead, it’s possible to obtain the initial state of the given contract by using the initOf
expression.
Maps
The type map<K, V>
is used as a way to associate keys of type K
with corresponding values of type V
.
Example of a map<K, V>
:
Learn more about them on a dedicated page: Maps.
Structs and Messages
Structs and Messages are two main ways of combining multiple primitive types into a composite one.
Example of a Struct:
Example of a Message:
Learn more about them on a dedicated page: Structs and Messages.
Optionals
All primitive types, as well as Structs and Messages could be nullable and hold a special null
value.
Example of an optional:
Learn more about them on a dedicated page: Optionals.
Contracts
Contracts in Tact serve as the main entrypoints of smart contracts of TON blockchain. They hold all functions, getters, and receivers of a TON contract, and much more.
Example of a contract:
Read more about them on the dedicated page: Contracts.
Traits
Tact doesn’t support classical class inheritance, but instead introduces the concept of traits, which can be viewed as abstract contracts (like abstract classes in popular object-oriented languages). They have the same structure as contracts, but can’t initialize persistent state variables.
A trait can also let the contract inheriting it to override the behavior of its functions and the value of its constants.
Example of a trait Ownable
from @stdlib/ownable
: