Files
Nim/tests/method/tmethod_various.nim
ringabout 30cf33f04d rework the vtable implementation embedding the vtable array directly with new strictions on methods (#22991)
**TODO**
- [x] fixes changelog
With the new option `nimPreviewVtables`, `methods` are confined in the
same module where the type of the first parameter is defined

- [x] make it opt in after CI checks its feasibility

## In the following-up PRs

- [ ] in the following PRs, refactor code into a more efficient one

- [ ] cpp needs special treatments since it cannot embed array in light
of the preceding limits: ref
https://github.com/nim-lang/Nim/pull/20977#discussion_r1035528927; we
can support cpp backends with vtable implementations later on the
comprise that uses indirect vtable access

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2023-11-28 15:11:43 +01:00

80 lines
1.3 KiB
Nim

discard """
matrix: "--mm:arc; --mm:refc"
output: '''
HELLO WORLD!
'''
"""
type
TNode* {.inheritable.} = object
PNode* = ref TNode
PNodeFoo* = ref object of TNode
TSomethingElse = object
PSomethingElse = ref TSomethingElse
method foo(a: PNode, b: PSomethingElse) {.base.} = discard
method foo(a: PNodeFoo, b: PSomethingElse) = discard
# tmproto
type
Obj1 {.inheritable.} = ref object
Obj2 = ref object of Obj1
method beta(x: Obj1): int {.base.}
proc delta(x: Obj2): int =
beta(x)
method beta(x: Obj2): int
proc alpha(x: Obj1): int =
beta(x)
method beta(x: Obj1): int = 1
method beta(x: Obj2): int = 2
proc gamma(x: Obj1): int =
beta(x)
doAssert alpha(Obj1()) == 1
doAssert gamma(Obj1()) == 1
doAssert alpha(Obj2()) == 2
doAssert gamma(Obj2()) == 2
doAssert delta(Obj2()) == 2
# tsimmeth
import strutils
var x = "hello world!".toLowerAscii.toUpperAscii
x.echo()
# trecmeth
# Note: We only compile this to verify that code generation
# for recursive methods works, no code is being executed
type Obj = ref object of RootObj
# Mutual recursion
method alpha(x: Obj) {.base.}
method beta(x: Obj) {.base.}
method alpha(x: Obj) =
beta(x)
method beta(x: Obj) =
alpha(x)
# Simple recursion
method gamma(x: Obj) {.base.} =
gamma(x)