Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

feat: empty init() function is present by default #221

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pages/book/contracts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ contract Example {
const StateDelivered: Int = 2;
const StateDisputed: Int = 3;

// no need to init constants
init() {}

get fun sum(): Int {
// access constants from anywhere
return GlobalConst1 + self.ContractConst1 + self.StatePaid;
Expand Down Expand Up @@ -111,6 +108,14 @@ contract Example {
}
```

If a contract doesn't have any persistent state variables, or they all have their default value specified, it may omit the `init(){:tact}` function declaration altogether. That's because unless explicitly declared, the empty `init(){:tact}` function is present by default in all contracts.

The following is an example of a valid empty contract:

```tact
contract IamEmptyAndIknowIt {}
```

<Callout>

To obtain initial state of the target contract in [internal functions](#internal-functions), [receivers](#receiver-functions) or [getters](#getter-functions) use [`initOf{:tact}`](/book/expressions#initof) expression.
Expand Down Expand Up @@ -201,7 +206,6 @@ They can only be called from [receivers](#receiver-functions), [getters](#getter
```tact
contract Functions {
val: Int = 0;
init() {}

// this contract method can only be called from within this contract and access its variables
fun onlyZeros() {
Expand Down
6 changes: 2 additions & 4 deletions pages/book/exit-codes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ Example:
1. First, define an empty contract like below:

```tact
contract Fireworks {
init() {}
}
contract Fireworks {}
```

2. Then, send a message to this contract. Because no suitable operation is found, you will get this exit code.
Expand Down Expand Up @@ -301,4 +299,4 @@ Example:
// fun newAddress(chain: Int, hash: Int): Address;
// creates a new address from chain and hash values.
let zeroAddress: Address = newAddress(-1, 0); // masterchain zero address
```
```
1 change: 0 additions & 1 deletion pages/book/expressions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ Anywhere in the function body, a global [static function](/book/functions#global

```tact
contract ExampleContract {
init() {}
receive() {
now(); // now() is a static function of stdlib
let expiration: Int = now() + 1000; // operation and variable declaration
Expand Down
3 changes: 0 additions & 3 deletions pages/book/maps.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,6 @@ contract Example {
arr: map<Int, Int>; // "array" of String values as a map
arrLength: Int = 0; // length of the "array", defaults to 0

// Constructor (initialization) function of the contract
init() {}

// Internal function for pushing an item to the end of the "array"
fun arrPush(item: String) {
if (self.arrLength >= self.MaxMapSize) {
Expand Down
2 changes: 0 additions & 2 deletions pages/book/types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ contract HelloWorld {
counter: Int;

// Constructor function init(), where all the variables are initialized
// Note, that an empty init function won't be needed starting with Tact 1.3.0,
// see: https://github.com/tact-lang/tact/pull/167
init() {
self.counter = 0;
}
Expand Down
Loading