Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Equality on addresses, slices, cells. #1279

Merged
merged 5 commits into from
Jan 8, 2025
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
23 changes: 21 additions & 2 deletions docs/src/content/docs/book/expressions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ contract ExampleContract {

## `initOf`

Expression `initOf{:tact}` computes initial state (`StateInit{:tact}`) of a [contract](/book/contracts):
Expression `initOf{:tact}` computes initial state, i.e. `StateInit{:tact}` of a [contract](/book/contracts):

```tact
// argument values for the init() function of the contract
Expand All @@ -263,13 +263,32 @@ initOf ExampleContract(
);
```

Where `StateInit{:tact}` is a built-in [Struct][s], that consists of:
The `StateInit{:tact}` is a built-in [Struct][s], that consists of:

Field | Type | Description
:----- | :----------------- | :----------
`code` | [`Cell{:tact}`][cell] | initial code of the [contract](/book/contracts) (the compiled bytecode)
`data` | [`Cell{:tact}`][cell] | initial data of the [contract](/book/contracts) (arguments of `init(){:tact}` function of the contract)

:::note

For workchain $0$, the [`Address{:tact}`][p] of the current contract obtained by calling the [`myAddress(){:tact}`](/ref/core-common#myaddress) function is identical to the one that can be obtained by calling the [`contractAddress(){:tact}`](/ref/core-common#contractaddress) function with the initial state of the current contract computed via `initOf{:tact}`:

```tact {6}
contract TheKingPrawn {
receive("keeping the address") {
let myAddr1 = myAddress();
let myAddr2 = contractAddress(initOf TheKingPrawn());

myAddr1 == myAddr2; // true
}
}
```

However, if you only need the address of the current contract at runtime and not its `StateInit{:tact}`, use the [`myAddress(){:tact}`](/ref/core-common#myaddress) function, as it consumes **significantly** less gas.

:::

[p]: /book/types#primitive-types
[cell]: /book/cells#cells
[s]: /book/structs-and-messages#structs
Expand Down
16 changes: 14 additions & 2 deletions docs/src/content/docs/book/operators.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ two / 1; // 2

:::note

Note that the following relationship between the division and modulo operators always holds for `Int{:tact}` type:
The following relationship between the division and [modulo](#binary-modulo) operators always holds for `Int{:tact}` type:

```tact
a / b * b + a % b == a; // true for any Int values of `a` and `b`,
Expand All @@ -247,12 +247,24 @@ two % 1; // 1
-1 % -5; // -1
```

The simplest way to avoid confusion between the two is to prefer using positive values via [`abs(x: Int){:tact}`](/ref/core-math#abs):
The simplest way to avoid confusing getting the modulo with getting the remainder is to [use only unsigned integers](/book/security-best-practices#misuse-of-signed-integers). Alternatively, consider using [`abs(){:tact}`](/ref/core-math#abs) function to ensure non-negative values:

```tact
abs(-1) % abs(-5); // 1
```

:::note

The following relationship between the [division](#binary-divide) and modulo operators always holds for `Int{:tact}` type:

```tact
a / b * b + a % b == a; // true for any Int values of `a` and `b`,
// except when `b` is equal to 0 and we divide `a` by 0,
// which is an attempt to divide by zero resulting in an error
```

:::

:::note

Did you know, that in JavaScript `%{:tact}` works as a _remainder_ operator, but not _modulo_ operator (like in Tact)?\
Expand Down
Loading
Loading