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

document --experimental:vtables #23111

Merged
merged 1 commit into from
Dec 21, 2023
Merged
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
35 changes: 35 additions & 0 deletions doc/manual_experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -2520,3 +2520,38 @@ NimFunctor()(1)
```
Notice we use the overload of `()` to have the same semantics in Nim, but on the `importcpp` we import the functor as a function.
This allows to easy interop with functions that accepts for example a `const` operator in its signature.

VTable for methods
==================

Methods now support implementations based on a VTable by using `--experimental:vtables`. Note that the option needs to enabled
globally. The virtual method table is stored in the type info of
an object, which is an array of function pointers.

```nim
method foo(x: Base, ...) {.base.}
method foo(x: Derived, ...) {.base.}
```

It roughly generates a dispatcher like

```nim
proc foo_dispatch(x: Base, ...) =
x.typeinfo.vtable[method_index](x, ...) # method_index is the index of the sorted order of a method
```

Methods are required to be in the same module where their type has been defined.

```nim
# types.nim
type
Base* = ref object
```

```nim
import types

method foo(x: Base) {.base.} = discard
```

It gives an error: method `foo` can be defined only in the same module with its type (Base).
Loading