Skip to content

Commit

Permalink
docs: add signatures for map methods (#1352)
Browse files Browse the repository at this point in the history
Co-authored-by: Anton Trunov <[email protected]>
  • Loading branch information
novusnota and anton-trunov authored Jan 16, 2025
1 parent 4747594 commit 9cd0675
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added a note on 255 being the maximum number of messages that can be sent during action phase: PR [#1237](https://github.com/tact-lang/tact/pull/1237)
- Added onchain metadata creation for NFTs and Jettons to the cookbook: PR [#1236](https://github.com/tact-lang/tact/pull/1236)
- Document that identifiers cannot start with `__gen` or `__tact`, and cannot contain Unicode characters apart from the small subset `a-zA-Z0-9_`: PR [#1312](https://github.com/tact-lang/tact/pull/1312)
- Added signatures for map methods, such as `.get()`, `.exists()`, `.set()`, `.replace()`, `.replaceGet()`, `.del()`, `.isEmpty()`, `.deepEquals()`, `.asCell()`: PR [#1352](https://github.com/tact-lang/tact/pull/1352)

### Release contributors

Expand Down
47 changes: 46 additions & 1 deletion docs/src/content/docs/book/maps.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ Note, that [persistent state variables](/book/contracts#variables) of type `map<

### Set values, `.set()` {#set}

```tact
// K and V correspond to the key and value types of the given map
extends mutates fun set(self: map<K, V>, key: K, val: V);
```

To set or replace the value under a key call the `.set(){:tact}` [method](/book/functions#extension-function), which is accessible for all maps.

```tact
Expand All @@ -91,7 +96,12 @@ fizz.set(7, 68); // key 7 now points to value 68

### Get values, `.get()` {#get}

To check if a key is found in the map by calling the `.get(){:tact}` [method](/book/functions#extension-function), which is accessible for all maps. This will return `null{:tact}` if the key is missing, or the value if the key is found.
```tact
// K and V correspond to the key and value types of the given map
extends fun get(self: map<K, V>, key: K): V?;
```

To check if a key is found in the map by calling the `.get(){:tact}` [method](/book/functions#extension-function), which is accessible for all maps. This will return [`null{:tact}`](/book/optionals) if the key is missing, or the value if the key is found.

```tact
// Empty map
Expand All @@ -118,6 +128,11 @@ if (gotButUnsure != null) {

<Badge text="Available since Tact 1.6 (not released yet)" variant="tip" size="medium"/><p/>

```tact
// K and V correspond to the key and value types of the given map
extends mutates fun replace(self: map<K, V>, key: K, val: V): Bool;
```

To replace the value under a key, if such a key exists, use the `.replace(){:tact}` [method](/book/functions#extension-function). It returns `true{:tact}` on successful replacement and `false{:tact}` otherwise.

```tact
Expand Down Expand Up @@ -160,6 +175,11 @@ replaced2; // false

<Badge text="Available since Tact 1.6 (not released yet)" variant="tip" size="medium"/><p/>

```tact
// K and V correspond to the key and value types of the given map
extends mutates fun replaceGet(self: map<K, V>, key: K, val: V): V?;
```

Like [`.replace()`](#replace), but instead of returning a [`Bool{:tact}`](/book/types#booleans) it returns the old (pre-replacement) value on successful replacement and [`null{:tact}`](/book/optionals) otherwise.

```tact
Expand Down Expand Up @@ -200,6 +220,11 @@ oldVal2; // null

### Delete entries, `.del()` {#del}

```tact
// K and V correspond to the key and value types of the given map
extends mutates fun del(self: map<K, V>, key: K): Bool;
```

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.

```tact
Expand Down Expand Up @@ -241,6 +266,11 @@ With this approach all previous entries of the map are completely discarded from

<Badge text="Available since Tact 1.5" variant="tip" size="medium"/><p/>

```tact
// K and V correspond to the key and value types of the given map
extends fun exists(self: map<K, V>, key: K): Bool;
```

The `.exists(){:tact}` [method](/book/functions#extension-function) on maps returns `true{:tact}` if the value under the given key exists in the map and `false{:tact}` otherwise.

```tact
Expand Down Expand Up @@ -268,6 +298,11 @@ if (fizz.get(1 / 2) != null) { // also true, but consumes more gas

### Check if empty, `.isEmpty()` {#isempty}

```tact
// K and V correspond to the key and value types of the given map
extends fun isEmpty(self: map<K, V>): Bool;
```

The `.isEmpty(){:tact}` [method](/book/functions#extension-function) on maps returns `true{:tact}` if the map is empty and `false{:tact}` otherwise:

```tact
Expand All @@ -288,6 +323,11 @@ if (fizz == null) {

<Badge text="Available since Tact 1.5" variant="tip" size="medium"/><p/>

```tact
// K and V correspond to the key and value types of the given map
extends fun deepEquals(self: map<K, V>, other: map<K, V>): Bool;
```

The `.deepEquals(){:tact}` [method](/book/functions#extension-function) on maps returns `true{:tact}` if all entries of the map match corresponding entries of another map, ignoring possible differences in the [underlying serialization logic][hashmap]. Returns `false{:tact}` otherwise.

```tact
Expand Down Expand Up @@ -331,6 +371,11 @@ There, both maps are formed manually and both contain the same key-value pair. I

### Convert to a `Cell`, `.asCell()` {#ascell}

```tact
// K and V correspond to the key and value types of the given map
extends fun asCell(self: map<K, V>): Cell;
```

On [TVM][tvm], maps are represented as a [`Cell{:tact}`][cell] type and it's possible to construct and parse them directly. However, doing so is highly error-prone and quite messy, which is why Tact provides maps as a standalone composite type with many of the helper methods mentioned above.

To cast maps back to the underlying [`Cell{:tact}`][cell] type, use the `.asCell(){:tact}` [method](/book/functions#extension-function). Since maps are initialized to `null{:tact}`, calling `.asCell(){:tact}` on a map with no values assigned will return `null{:tact}` and **not** an empty [`Cell{:tact}`][cell].
Expand Down

0 comments on commit 9cd0675

Please sign in to comment.