You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 12, 2024. It is now read-only.
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.
112
+
113
+
The following is an example of a valid empty contract:
114
+
115
+
```tact
116
+
contract IamEmptyAndIknowIt {}
117
+
```
118
+
114
119
<Callout>
115
120
116
121
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.
@@ -201,7 +206,6 @@ They can only be called from [receivers](#receiver-functions), [getters](#getter
201
206
```tact
202
207
contract Functions {
203
208
val: Int = 0;
204
-
init() {}
205
209
206
210
// this contract method can only be called from within this contract and access its variables
Copy file name to clipboardExpand all lines: pages/book/maps.mdx
+48-9Lines changed: 48 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ Allowed value types:
30
30
31
31
## Operations
32
32
33
-
### Declare
33
+
### Declare, `emptyMap()`[#emptymap]
34
34
35
35
As a [local variable](/book/statements#let), using `emptyMap(){:tact}` function of standard library:
36
36
@@ -93,9 +93,9 @@ if (gotButUnsure != null) {
93
93
}
94
94
```
95
95
96
-
### Delete entries [#del]
96
+
### Delete entries, `.del()`[#del]
97
97
98
-
To delete a single key-value pair (single entry), simply assign the `null{:tact}` value to the key when using the [`.set(){:tact}`](#set)[method](/book/functions#extension-function).
98
+
To delete a single key-value pair (single entry), use the `.del(){:tact}`[method](/book/functions#extension-function). It returns `true{:tact}` in the case of successful deletion and `false{:tact}` otherwise.
99
99
100
100
```tact
101
101
// Empty map
@@ -106,7 +106,13 @@ fizz.set(7, 123);
106
106
fizz.set(42, 321);
107
107
108
108
// Deleting one of the keys
109
-
fizz.set(7, null); // the entry under key 7 is now deleted
109
+
let deletionSuccess: Bool = fizz.del(7); // true, because map contained the entry under key 7
110
+
fizz.del(7); // false, because map no longer has an entry under key 7
111
+
112
+
// Note, that assigning the `null` value to the key when using the `.set()` method
113
+
// is equivalent to calling `.del()`, although such approach is much less descriptive
114
+
// and is generally discouraged:
115
+
fizz.set(42, null); // the entry under key 42 is now deleted
110
116
```
111
117
112
118
To delete all the entries from the map, re-assign the map using the `emptyMap(){:tact}` function:
@@ -126,7 +132,25 @@ fizz = null; // identical to the previous line, but less descriptive
126
132
127
133
With this approach all previous entries of the map are completely discarded from the contract even if the map was declared as its persistent state variable. As a result, assigning maps to `emptyMap(){:tact}`**does not** inflict any hidden or sudden [storage fees](https://docs.ton.org/develop/smart-contracts/fees#storage-fee).
128
134
129
-
### Convert to a `Cell`, `.asCell()`[#convert]
135
+
### Check if empty, `.isEmpty()`[#isempty]
136
+
137
+
The `.isEmpty(){:tact}`[method](/book/functions#extension-function) on maps returns `true{:tact}` if the map is empty and `false{:tact}` otherwise:
138
+
139
+
```tact
140
+
let fizz: map<Int, Int> = emptyMap();
141
+
142
+
if (fizz.isEmpty()) {
143
+
dump("Empty maps are empty, duh!");
144
+
}
145
+
146
+
// Note, that comparing the map to `null` behaves the same as `.isEmpty()` method,
147
+
// although such direct comparison is much less descriptive and is generally discouraged:
148
+
if (fizz == null) {
149
+
dump("Empty maps are null, which isn't obvious");
150
+
}
151
+
```
152
+
153
+
### Convert to a `Cell`, `.asCell()`[#ascell]
130
154
131
155
Use `.asCell(){:tact}`[method](/book/functions#extension-function) on maps to convert all their values to a [`Cell{:tact}`][p] type. Be mindful, that [`Cell{:tact}`][p] type is able to store up to 1023 bits, so converting larger maps to the Cell will result in error.
132
156
@@ -157,7 +181,25 @@ contract Example {
157
181
158
182
### Traverse over entries [#traverse]
159
183
160
-
At the moment Tact doesn't have a special syntax for iterating over maps. However, it's possible to use maps as a simple arrays if you define a `map<Int, v>{:tact}` with an [`Int{:tact}`][int] type for the keys and keep track of the number of items in the separate variable:
184
+
To iterate over map entries there is a [`foreach{:tact}`](/book/statements#foreach-loop) loop statement:
185
+
186
+
```tact
187
+
// Empty map
188
+
let fizz: map<Int, Int> = emptyMap();
189
+
190
+
// Setting a couple of values under different keys
191
+
fizz.set(42, 321);
192
+
fizz.set(7, 123);
193
+
194
+
// Iterating over in a sequential order: from the smallest keys to the biggest ones
195
+
foreach (key, value in fizz) {
196
+
dump(key); // will dump 7 on the first iteration, then 42 on the second
197
+
}
198
+
```
199
+
200
+
Read more about it: [`foreach{:tact}` loop in Book→Statements](/book/statements#foreach-loop).
201
+
202
+
Note, that it's also possible to use maps as simple arrays if you define a `map<Int, V>{:tact}` with an [`Int{:tact}`][int] type for the keys, any allowed `V` type for values and keep track of the number of items in the separate variable:
161
203
162
204
```tact
163
205
contract Iteration {
@@ -265,9 +307,6 @@ contract Example {
265
307
arr: map<Int, Int>; // "array" of String values as a map
266
308
arrLength: Int = 0; // length of the "array", defaults to 0
267
309
268
-
// Constructor (initialization) function of the contract
269
-
init() {}
270
-
271
310
// Internal function for pushing an item to the end of the "array"
Copy file name to clipboardExpand all lines: pages/book/message-mode.mdx
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ $+32$ | `SendDestroyIfZero{:tact}` | Current account must be destroyed
25
25
26
26
## Combining modes with flags
27
27
28
-
To make the [`Int{:tact}`][int] value for `mode` field of SendParameters, you just have to combine base modes with optional flags by applying the [`bitwise OR{:tact}`](/book/operators#binary-bitwise-or) operation.
28
+
To make the [`Int{:tact}`][int] value for `mode` field of `SendParameters{:tact}`, you just have to combine base modes with optional flags by applying the [`bitwise OR{:tact}`](/book/operators#binary-bitwise-or) operation.
29
29
30
30
For example, if you want to send a regular message and pay transfer fees separately, use the mode $0$ (default) and a flag $+1$ to get `mode` $= 1$, which is equal to using `SendPayGasSeparately{:tact}` constant.
0 commit comments