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

Commit 5221dfb

Browse files
authored
feat: .isEmpty() method for maps (#218)
And minor adjustments to the headers and ToC
1 parent 3609ef9 commit 5221dfb

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

pages/book/maps.mdx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Allowed value types:
3030

3131
## Operations
3232

33-
### Declare
33+
### Declare, `emptyMap()` [#emptymap]
3434

3535
As a [local variable](/book/statements#let), using `emptyMap(){:tact}` function of standard library:
3636

@@ -126,7 +126,25 @@ fizz = null; // identical to the previous line, but less descriptive
126126

127127
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).
128128

129-
### Convert to a `Cell`, `.asCell()` [#convert]
129+
### Check if empty, `.isEmpty()` [#isempty]
130+
131+
The `.isEmpty(){:tact}` [method](/book/functions#extension-function) on maps returns `true{:tact}` if the map is empty and `false{:tact}` otherwise:
132+
133+
```tact
134+
let fizz: map<Int, Int> = emptyMap();
135+
136+
if (fizz.isEmpty()) {
137+
dump("Empty maps are empty, duh!");
138+
}
139+
140+
// Note, that comparing the map to `null` behaves the same as `.isEmpty()` method,
141+
// although such direct comparison is much less descriptive and is generally discouraged:
142+
if (fizz == null) {
143+
dump("Empty maps are null, which isn't obvious");
144+
}
145+
```
146+
147+
### Convert to a `Cell`, `.asCell()` [#ascell]
130148

131149
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.
132150

0 commit comments

Comments
 (0)