Defining composite types
Tact supports a number of primitive data types that are tailored for smart contract use. However, using individual means of storage often becomes cumbersome, so there are two main ways to combine multiple primitives together: Structs and Messages.
Note, while Traits and Contracts are also considered a part of the Tacts type system, one can't pass them around like Structs or Messages. Instead, one can obtain the initial state of the given Contract by using the initOf statement described later in the Book.
Warning: Currently circular types are not possible. This means that struct/message A can't have a field of a struct/message B that has a field of the struct/message A.
Therefore, the following code won't compile:
struct A {
circularFieldA: B;
}
struct B {
impossibleFieldB: A;
}
Structs
Structs can define complex data types that contain multiple fields of different types. They can also be nested.
struct Point {
x: Int as int64;
y: Int as int64;
}
struct Line {
start: Point;
end: Point;
}
Structs can also include both default fields and optional fields. This can be quite useful when you have many fields but don't want to keep respecifying them.
struct Params {
name: String = "Satoshi"; // default value
age: Int?; // optional field
point: Point; // nested Structs
}
Structs are also useful as return values from getters or other internal functions. They effectively allow a single getter to return multiple return values.
contract StructsShowcase {
params: Params; // Struct as a Contract persistent state variable
init() {
self.params = Params{point: Point{x: 4, y: 2}};
}
get fun params(): Params {
return self.params;
}
}
The order of fields does not matter. Unlike other languages, Tact does not have any padding between fields.
Messages
Messages can hold Structs in them:
struct Point {
x: Int;
y: Int;
}
message Add {
point: Point; // holds a struct Point
}
Messages are almost the same thing as Structs with the only difference that Messages have a 32-bit integer header in their serialization containing their unique numeric id. This allows Messages to be used with receivers since the contract can tell different types of messages apart based on this id.
Tact automatically generates those unique ids for every received Message, but this can be manually overwritten:
// This Message overwrites its unique id with 0x7362d09c
message(0x7362d09c) TokenNotification {
forwardPayload: Slice as remaining;
}
This is useful for cases where you want to handle certain opcodes (operation codes) of a given smart contract, such as Jetton standard (opens in a new tab). The short-list of opcodes this contract is able to process is given here in FunC (opens in a new tab). They serve as an interface to the smart contract.
Optionals
As it was mentioned in type system overview, most primitive types, Structs and Messages could be nullable. That is, they don't necessarily hold any value, aside from null
— a special value, which represents the intentional absence of any other value.
Variables or fields of Structs and Messages that can hold null
are called "optionals". They're useful to reduce state size when the variable isn't necesserily used.
You can make any variable an optional by adding a question mark (?
) after its type declaration. The only exceptions are map<>
and bounced<>
, where you can't make them, inner key/value type (in case of a map) or the inner Message (in case of a bounced) optional.
Optional variables that are not defined hold the null
value by default. You cannot access them without checking for null
first. But if you're certain the optional variable is not null
, use the non-null assertion operator (!!
, also called double-bang or double exclamation mark operator) to access its value.
Trying to access the value of an optional variable without using !!
or checking for null
beforehand will result in a compilation error.
Example of optionals:
struct StOpt {
opt: Int?; // Int or null
}
message MsOpt {
opt: StOpt?; // Notice, how the struct StOpt is used in this definition
}
contract Optionals {
opt: Int?;
address: Address?;
init(opt: Int?) { // optionals as parameters
self.opt = opt;
self.address = null; // explicit null value
}
receive(msg: MsOpt) {
let opt: Int? = 12; // defining a new variable
if (self.opt != null) { // explicit check
self.opt = opt!!; // using !! as we know that opt value isn't null
}
}
}