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 (opens in a new tab) in TON Blockchain.Cell
,Builder
,Slice
— low-level primitives of TVM (opens in a new tab).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 (opens in a new tab) 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>
:
let mapExample: map<Int, Int> = emptyMap(); // empty map with Int keys and values
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:
struct Point {
x: Int;
y: Int;
}
Example of a Message:
// Custom numeric id of the Message
message(0x11111111) SetValue {
key: Int;
value: Int?; // Optional, Int or null
coins: Int as coins; // Serialization into TL-B types
}
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:
let opt: Int? = null; // Int or null, with explicitly assigned null
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:
contract HelloWorld {
// Persistent state variable
counter: Int;
// Constructor function init(), where all the variables are initialized
init() {
self.counter = 0;
}
// Internal message receiver, which responds to a string message "increment"
receive("increment") {
self.counter += 1;
}
// Getter function with return type Int
get fun counter(): Int {
return self.counter;
}
}
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
:
trait Ownable {
// Persistent state variable, which cannot be initialized in the trait
owner: Address;
// Internal function
fun requireOwner() {
nativeThrowUnless(132, context().sender == self.owner);
}
// Getter function with return type Address
get fun owner(): Address {
return self.owner;
}
}
And the contract that uses trait Ownable
:
contract Treasure with Ownable {
// Persistent state variable, which MUST be defined in the contract
owner: Address;
// Constructor function init(), where all the variables are initialized
init(owner: Address) {
self.owner = owner;
}
}