Functions
Functions in Tact could be defined in different ways:
- Global static function
- Extension functions
- Mutation functions
- Native functions
- Assembly functions
- Internal functions
- Receiver functions
- Getter functions
All functions, except for receiver functions can have a trailing comma in their definitions (parameter lists) and calls (argument lists):
Global static functions
You can define global function anywhere in your program:
Virtual and abstract functions
You can allow the contract inheriting a traits to modify an internal function, if it has the virtual
keyword, using override
. The function can be also marked as abstract
, in which case the inheriting contract has to define its implementation:
Extension function
Extension functions allow you to implement extensions for any possible type.
Warning The name of the first argument MUST be named
self
and the type of this argument is the type you are extending.
Mutation functions
Mutation functions are performing mutation of a value replacing it with an execution result. To perform mutation, the function must change the self
value.
Native functions
Native functions are direct bindings of FunC functions:
Note Native functions could also be mutation and extension ones.
Assembly functions, asm
Available since Tact 1.5
Read more about them on their dedicated page: Assembly functions.
Receiver functions
Receiver functions are special functions that are responsible for receiving messages in contracts and could be defined only within a contract or trait.
Getter Functions
Getter functions define getters on smart contracts and can be defined only within a contract or trait. Getter functions cannot be used to read some other contract’s state: if you need to obtain some data you need to do that by sending a message with a request and define a receiver which would process the request answer.
Explicit resolution of method ID collisions
Available since Tact 1.6Like other functions in TON contracts, getters have their unique associated function selectors, which are -bit signed integer identifiers commonly called method IDs.
Method IDs of getters are derived from their names using the CRC16 algorithm as follows: (crc16(<function_name>) & 0xffff) | 0x10000
. In addition, Tact compiler conditionally reserves some method IDs for use in getters of supported interfaces, namely: for supported_interfaces
, for lazy_deployment_completed
, and for get_abi_ipfs
.
Sometimes, getters with different names end up with the same method ID. If this happens, you can either rename some of the getters or manually specify the method ID as a compile-time expression like so:
Unlike getters, method IDs for internal functions and some special functions are obtained sequentially: integers in the inclusive range from to are given to certain message handlers, while internal functions are numbered with method IDs starting at and going up to inclusive.
Since method IDs are -bit signed integers and some of them are reserved, only the inclusive ranges from to and from to are free to be used by users. To avoid collisions, it’s recommended to specify method IDs only in these ranges, avoiding the method IDs of Tact-specific getters mentioned above.