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

Commit

Permalink
feat: describe inheritance via with keyword and BaseTrait (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
novusnota authored Jul 23, 2024
1 parent e33e726 commit 80833bc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"mathrm",
"nanotons",
"nextra",
"omelander", // some superhero
"Offchain",
"quadtree",
"quadtrees",
Expand Down
35 changes: 34 additions & 1 deletion pages/book/contracts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ contract Example {

Each contract can contain:

* [Inherited traits](#traits)
* [Supported interfaces](#interfaces)
* [Persistent state variables](#variables)
* [Constructor function `init(){:tact}`](#init-function)
Expand All @@ -31,7 +32,39 @@ Each contract can contain:
* [Receiver functions](#receiver-functions)
* [Internal functions](#internal-functions)

Furthermore, contracts can inherit all the declarations and definitions from [traits][trait] and override some of their default behaviours. If declared or defined in a trait, internal functions and constants can be marked as [virtual or abstract](/book/functions#virtual-and-abstract-functions) and overridden in contracts inheriting from the trait.
### Inherited traits, `with{:tact}` [#traits]

Contracts can inherit all the declarations and definitions from [traits][trait] and override some of their default behaviours. In addition to that, every contract and trait implicitly inherits the special [`BaseTrait{:tact}` trait](/ref/core-base).

To inherit a [trait][trait], specify its name after the keyword `with{:tact}` in contract's declaration. To inherit multiple traits at once, specify their names in a comma-separated list with an optional trailing comma.

```tact /with/
trait InheritMe {}
trait InheritMeToo {}
// A contract inheriting a single trait
contract Single with InheritMe {}
// A contract inheriting multiple traits
contract Plural with
InheritMe,
InheritMeToo, // trailing comma is allowed
{}
```

As [traits][trait] are not allowed to have [`init(){:tact}` function](#init-function), a contract inheriting a trait with any [persistent state variables](#variables) declared must initialize them by providing its own [`init(){:tact}` function](#init-function).

```tact
trait Supe { omelander: Bool }
contract Vot with Supe {
init() {
self.omelander = true;
}
}
```

If declared or defined in a trait, internal functions and constants can be marked as [virtual or abstract](/book/functions#virtual-and-abstract-functions) and overridden in contracts inheriting from the trait.

### Supported interfaces, `@interface(…)` [#interfaces]

Expand Down
2 changes: 1 addition & 1 deletion pages/ref/core-base.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Callout } from 'nextra-theme-docs'

Every [contract](/book/contracts) and [trait](/book/types#traits) in Tact implicitly inherits the `Base{:tact}` trait, which contains a number of the most useful [internal functions](/book/contracts#internal-functions) for any kind of contract, and a constant `self.storageReserve{:tact}` aimed at advanced users of Tact.
Every [contract](/book/contracts) and [trait](/book/types#traits) in Tact implicitly [inherits](/book/contracts#traits) the `BaseTrait{:tact}` trait, which contains a number of the most useful [internal functions](/book/contracts#internal-functions) for any kind of contract, and a constant `self.storageReserve{:tact}` aimed at advanced users of Tact.

## Constants

Expand Down

0 comments on commit 80833bc

Please sign in to comment.