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

Add details regarding abstract and virtual functions in traits #207

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion pages/book/contracts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ contract HelloWorld {

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".

Internal functions can access the contract's [persistent state variables](#variables) and [constants](#constants).
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.

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).

Expand Down
28 changes: 28 additions & 0 deletions pages/book/functions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ fun pow(a: Int, c: Int): Int {
}
```

## Virtual and abstract functions

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:

```tact
trait FilterTrait with Ownable {
virtual fun filterMessage(): Bool { // virtual functions can be overridden by users of this trait
if (sender() == self.owner) {
return false;
}
return true;
}

abstract fun specialFilter(): Bool;
}

contract Filter with FilterTrait {
// the trait allows us to override the default behavior
override fun filterMessage(): Bool {
return true;
}

override fun specialFilter(): Bool {
return true;
}
}
````

## Extension function

Extension functions allow you to implement extensions for any possible type.
Expand Down
4 changes: 3 additions & 1 deletion pages/book/types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ Read more about them on the dedicated page: [Contracts](/book/contracts).

### Traits

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.
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).

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).

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

Expand Down
Loading