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

feat(docs): explain how storage variables get updated #1311

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed links in Chinese translation: PR [#1206](https://github.com/tact-lang/tact/pull/1206)
- 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 how storage variables get updated in relation to the `init()` function: PR [#1310](https://github.com/tact-lang/tact/pull/1310)

### Release contributors

Expand Down
34 changes: 31 additions & 3 deletions docs/src/content/docs/book/contracts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ In addition to that, [`tact.config.json`](/book/config) may still be used in [Bl

### Persistent state variables {#variables}

Contracts can define state variables that persist between contract calls. Contracts in TON [pay rent](https://docs.ton.org/develop/smart-contracts/fees#storage-fee) in proportion to the amount of persistent space they consume, so [compact representations via serialization](/book/integers#serialization) are encouraged.
Contracts can define state variables that persist between contract calls, and thus commonly referred to as _storage_ variables. Contracts in TON [pay rent](https://docs.ton.org/develop/smart-contracts/fees#storage-fee) in proportion to the amount of persistent space they consume, so [compact representations via serialization](/book/integers#serialization) are encouraged.

```tact
contract Example {
Expand All @@ -149,11 +149,25 @@ contract Example {
}
```

State variables must have a default value or initialized in [`init(){:tact}`](#init-function) function, that runs on deployment of the contract. The only exception is persistent state variables of type [`map<K, V>{:tact}`](/book/maps) since they are initialized empty by default.
State variables must have a default value or be initialized in the [`init(){:tact}`](#init-function) function that runs once on deployment of the contract. The only exception are persistent state variables of type [`map<K, V>{:tact}`](/book/maps), since they are initialized empty by default.

The default value of state variables is assigned before any values could be assigned in the [`init(){:tact}`](#init-function) function.

```tact
contract Example {
// persistent state variables
var1: Int = 0; // initialized with default value 0

// constructor function
init() {
self.var1 = 42; // overrides the default to 42
}
}
```

:::note

Note, that Tact supports local, non-persistent-state variables too, see: [Variable declaration](/book/statements#let).
Tact supports local, non-persistent-state variables too, see: [Variable declaration](/book/statements#let).

:::

Expand Down Expand Up @@ -211,6 +225,20 @@ contract Example {
}
```

The default value of [state variables](#variables) is assigned before any values could be assigned in the `init(){:tact}` function.

```tact
contract Example {
// persistent state variables
var1: Int = 0; // initialized with default value 0

// constructor function
init() {
self.var1 = 42; // overrides the default to 42
}
}
```

Comment on lines +228 to +241
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a duplicate of the example above

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the intent, but yeah, I'll remove it

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.

The following is an example of a valid empty contract:
Expand Down
Loading