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

Commit 4c9d4e9

Browse files
committed
Add details regarding abstract and virtual functions in traits
1 parent bd8aa96 commit 4c9d4e9

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

pages/book/contracts.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ contract HelloWorld {
194194

195195
These functions behave similarly to private methods in popular object-oriented languages — they're internal to contracts and can be called by prefixing them with a special [identifier `self{:tact}`](#field-access). That's why internal functions can sometimes be referred to as "contract methods".
196196

197-
Internal functions can access the contract's [persistent state variables](#variables) and [constants](#constants).
197+
Internal functions can access the contract's [persistent state variables](#variables) and [constants](#constants). If declared in a [traits](/book/types#traits), they can be declared as [virtual or abstract](/book/functions#virtual-and-abstract-functions) and overriden in the contract inheriting it.
198198

199199
They can only be called from [receivers](#receiver-functions), [getters](#getter-functions) and other internal functions, but not from other contracts or [`init(){:tact}`](#init-function).
200200

pages/book/functions.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,34 @@ 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+
if (sender() == self.owner) {
34+
return false;
35+
}
36+
return true;
37+
}
38+
39+
abstract fun specialFilter(): Bool;
40+
}
41+
42+
contract Filter with FilterTrait {
43+
// the trait allows us to override the default behavior
44+
override fun filterMessage(): Bool {
45+
return true;
46+
}
47+
48+
override fun specialFilter(): Bool {
49+
return true;
50+
}
51+
}
52+
````
53+
2654
## Extension function
2755
2856
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
@@ -138,7 +138,9 @@ Read more about them on the dedicated page: [Contracts](/book/contracts).
138138

139139
### Traits
140140

141-
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.
141+
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).
142+
143+
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).
142144

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

0 commit comments

Comments
 (0)