Skip to content

Commit

Permalink
Merge branch 'main' into 988-deafult-send-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov authored Oct 31, 2024
2 parents 2186d30 + b571ec1 commit 4f9e401
Show file tree
Hide file tree
Showing 10 changed files with 6,586 additions and 2,941 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Docs: automatic links to Web IDE from all code blocks: PR [#994](https://github.com/tact-lang/tact/pull/994)
- The `SendDefaultMode` send mode constant to the standard library: PR [#1010](https://github.com/tact-lang/tact/pull/1010)
- Docs: initial semi-automated Chinese translation of the documentation: PR [#942](https://github.com/tact-lang/tact/pull/942)
- The `replace` and `replaceGet` methods for the `Map` type: PR [#941](https://github.com/tact-lang/tact/pull/941)

### Changed

Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"rangle",
"rawslice",
"renamer",
"replaceget",
"rparen",
"rugpull",
"rugpulled",
Expand Down
84 changes: 84 additions & 0 deletions docs/src/content/docs/book/maps.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,90 @@ if (gotButUnsure != null) {
}
```

### Replace values, `.replace()` {#replace}

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

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
// Empty map
let fizz: map<Int, Int> = emptyMap();
// Setting a couple of values under different keys
fizz.set(7, 70);
fizz.set(42, 42);
// Overriding one of the existing key-value pairs
let replaced1 = fizz.replace(7, 68); // key 7 now points to value 68
replaced1; // true
// Trying to replace the value in a non-existing key-value pair will do nothing
let replaced2 = fizz.replace(8, 68); // no key 8, so nothing was altered
replaced2; // false
```

If the given value is [`null{:tact}`](/book/optionals) and the key exists, the entry will be deleted from the map.

```tact
// Empty map
let fizz: map<Int, Int> = emptyMap();
// Setting a couple of values under different keys
fizz.set(7, 70);
fizz.set(42, 42);
// Overriding one of the existing key-value pairs
let replaced1 = fizz.replace(7, null); // the entry under key 7 is now deleted
replaced1; // true
// Trying to replace the value in a non-existing key-value pair will do nothing
let replaced2 = fizz.replace(8, null); // no key 8, so nothing was altered
replaced2; // false
```

### Replace and get old value, `.replaceGet()` {#replaceget}

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

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
// Empty map
let fizz: map<Int, Int> = emptyMap();
// Setting a couple of values under different keys
fizz.set(7, 70);
fizz.set(42, 42);
// Overriding one of the existing key-value pairs
let oldVal1 = fizz.replaceGet(7, 68); // key 7 now points to value 68
oldVal1; // 70
// Trying to replace the value in a non-existing key-value pair will do nothing
let oldVal2 = fizz.replaceGet(8, 68); // no key 8, so nothing was altered
oldVal2; // null
```

If the given value is [`null{:tact}`](/book/optionals) and the key exists, the entry will be deleted from the map.

```tact
// Empty map
let fizz: map<Int, Int> = emptyMap();
// Setting a couple of values under different keys
fizz.set(7, 70);
fizz.set(42, 42);
// Overriding one of the existing key-value pairs
let oldVal1 = fizz.replaceGet(7, null); // the entry under key 7 is now deleted
oldVal1; // 70
// Trying to replace the value in a non-existing key-value pair will do nothing
let oldVal2 = fizz.replaceGet(8, null); // no key 8, so nothing was altered
oldVal2; // null
```

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

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.
Expand Down
Loading

0 comments on commit 4f9e401

Please sign in to comment.