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

Commit bf4403d

Browse files
authored
Add details regarding abstract and virtual functions in traits (#207)
1 parent 5a82ca0 commit bf4403d

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

pages/book/contracts.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Each contract can contain:
3030
* [Receiver functions](#receiver-functions)
3131
* [Internal functions](#internal-functions)
3232

33-
Furthermore, contracts can inherit all the declarations from [traits](/book/types#traits) and override some of their default behaviours.
33+
Furthermore, contracts can inherit all the declarations and definitions from [traits](/book/types#traits) 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 overriden in contracts inheriting from the trait.
3434

3535
### Persistent state variables [#variables]
3636

pages/book/functions.mdx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ fun pow(a: Int, c: Int): Int {
2323
}
2424
```
2525

26+
## Virtual and abstract functions
27+
28+
You can allow the contract inheriting a [traits](/book/types#traits) to modify an internal function, if it has the `virtual` keyword, using `override`. The function can be also marked as `abstract`, in which case the inheriting contract has to define its implementation:
29+
30+
```tact
31+
trait FilterTrait with Ownable {
32+
virtual fun filterMessage(): Bool { // virtual functions can be overridden by users of this trait
33+
return sender() != self.owner;
34+
}
35+
36+
abstract fun specialFilter(): Bool;
37+
}
38+
39+
contract Filter with FilterTrait {
40+
// the trait allows us to override the default behavior
41+
override fun filterMessage(): Bool {
42+
return true;
43+
}
44+
45+
override fun specialFilter(): Bool {
46+
return true;
47+
}
48+
}
49+
````
50+
2651
## Extension function
2752
2853
Extension functions allow you to implement extensions for any possible type.

pages/book/types.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ Read more about them on the dedicated page: [Contracts](/book/contracts).
136136

137137
### Traits
138138

139-
Tact doesn't support classical class inheritance, but instead introduces the concept of _traits_, which can be viewed as abstract contracts (like abstract classes in popular object-oriented languages). They have the same structure as [contracts](#contracts), but can't [initialize persistent state variables](/book/contracts#init-function), while allowing to override some of their behaviors.
139+
Tact doesn't support classical class inheritance, but instead introduces the concept of _traits_, which can be viewed as abstract contracts (like abstract classes in popular object-oriented languages). They have the same structure as [contracts](#contracts), but can't [initialize persistent state variables](/book/contracts#init-function).
140+
141+
A trait can also let the contract inheriting it to override the behavior of its [functions](/book/functions#virtual-and-abstract-functions) and the value of its [constants](/book/constants#virtual-and-abstract-constants).
140142

141143
Example of a trait [`Ownable`](/language/libs/ownable#ownable) from [`@stdlib/ownable`](/language/libs/ownable):
142144

0 commit comments

Comments
 (0)