diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2023-12-21 15:56:02 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-21 08:56:02 +0100 |
commit | b15463948c6f78a546742fc5c521fa2f6e8f9312 (patch) | |
tree | ae646358e2b292a538706256d1471645d17559e5 /doc | |
parent | 4321ce2635cc91a975580e8a74bff70cf5d8aadf (diff) | |
download | Nim-b15463948c6f78a546742fc5c521fa2f6e8f9312.tar.gz |
document `--experimental:vtables` (#23111)
Diffstat (limited to 'doc')
-rw-r--r-- | doc/manual_experimental.md | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/doc/manual_experimental.md b/doc/manual_experimental.md index 87381f655..fc5ff1959 100644 --- a/doc/manual_experimental.md +++ b/doc/manual_experimental.md @@ -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). |