parent
d67a9f024e
commit
ef8ddef47b
3 changed files with 54 additions and 6 deletions
|
|
@ -56,7 +56,7 @@ proc methodCall*(n: PNode; conf: ConfigRef): PNode =
|
||||||
type
|
type
|
||||||
MethodResult = enum No, Invalid, Yes
|
MethodResult = enum No, Invalid, Yes
|
||||||
|
|
||||||
proc sameMethodBucket(a, b: PSym): MethodResult =
|
proc sameMethodBucket(a, b: PSym; multiMethods: bool): MethodResult =
|
||||||
if a.name.id != b.name.id: return
|
if a.name.id != b.name.id: return
|
||||||
if sonsLen(a.typ) != sonsLen(b.typ):
|
if sonsLen(a.typ) != sonsLen(b.typ):
|
||||||
return
|
return
|
||||||
|
|
@ -75,7 +75,7 @@ proc sameMethodBucket(a, b: PSym): MethodResult =
|
||||||
if sameType(a.typ.sons[i], b.typ.sons[i]):
|
if sameType(a.typ.sons[i], b.typ.sons[i]):
|
||||||
if aa.kind == tyObject and result != Invalid:
|
if aa.kind == tyObject and result != Invalid:
|
||||||
result = Yes
|
result = Yes
|
||||||
elif aa.kind == tyObject and bb.kind == tyObject:
|
elif aa.kind == tyObject and bb.kind == tyObject and (i == 1 or multiMethods):
|
||||||
let diff = inheritanceDiff(bb, aa)
|
let diff = inheritanceDiff(bb, aa)
|
||||||
if diff < 0:
|
if diff < 0:
|
||||||
if result != Invalid:
|
if result != Invalid:
|
||||||
|
|
@ -162,7 +162,7 @@ proc methodDef*(g: ModuleGraph; s: PSym, fromCache: bool) =
|
||||||
var witness: PSym
|
var witness: PSym
|
||||||
for i in 0 ..< L:
|
for i in 0 ..< L:
|
||||||
let disp = g.methods[i].dispatcher
|
let disp = g.methods[i].dispatcher
|
||||||
case sameMethodBucket(disp, s)
|
case sameMethodBucket(disp, s, multimethods = optMultiMethods in g.config.globalOptions)
|
||||||
of Yes:
|
of Yes:
|
||||||
add(g.methods[i].methods, s)
|
add(g.methods[i].methods, s)
|
||||||
attachDispatcher(s, disp.ast[dispatcherPos])
|
attachDispatcher(s, disp.ast[dispatcherPos])
|
||||||
|
|
|
||||||
|
|
@ -277,7 +277,7 @@ Procedures always use static dispatch. For dynamic dispatch replace the
|
||||||
a, b: Expression
|
a, b: Expression
|
||||||
|
|
||||||
# watch out: 'eval' relies on dynamic binding
|
# watch out: 'eval' relies on dynamic binding
|
||||||
method eval(e: Expression): int =
|
method eval(e: Expression): int {.base.} =
|
||||||
# override this base method
|
# override this base method
|
||||||
quit "to override!"
|
quit "to override!"
|
||||||
|
|
||||||
|
|
@ -300,7 +300,7 @@ In a multi-method all parameters that have an object type are used for the
|
||||||
dispatching:
|
dispatching:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
:test: "nim c $1"
|
:test: "nim c --multiMethods:on $1"
|
||||||
|
|
||||||
type
|
type
|
||||||
Thing = ref object of RootObj
|
Thing = ref object of RootObj
|
||||||
|
|
@ -611,7 +611,7 @@ To pass a block of statements to a template, use 'untyped' for the last paramete
|
||||||
:test: "nim c $1"
|
:test: "nim c $1"
|
||||||
|
|
||||||
template withFile(f: untyped, filename: string, mode: FileMode,
|
template withFile(f: untyped, filename: string, mode: FileMode,
|
||||||
body: untyped): typed =
|
body: untyped) =
|
||||||
let fn = filename
|
let fn = filename
|
||||||
var f: File
|
var f: File
|
||||||
if open(f, fn, mode):
|
if open(f, fn, mode):
|
||||||
|
|
|
||||||
48
tests/method/tsingle_methods.nim
Normal file
48
tests/method/tsingle_methods.nim
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
discard """
|
||||||
|
cmd: "nim c --multimethods:off $file"
|
||||||
|
output: '''base
|
||||||
|
base
|
||||||
|
base
|
||||||
|
base
|
||||||
|
base
|
||||||
|
base
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
# bug #10912
|
||||||
|
|
||||||
|
type
|
||||||
|
X = ref object of RootObj
|
||||||
|
|
||||||
|
type
|
||||||
|
A* = ref object of RootObj
|
||||||
|
B* = ref object of A
|
||||||
|
C* = ref object of A
|
||||||
|
D* = ref object of A
|
||||||
|
E* = ref object of A
|
||||||
|
F* = ref object of A
|
||||||
|
|
||||||
|
method resolve(self: var X, stmt: A) {.base.} = echo "base"
|
||||||
|
|
||||||
|
proc resolveSeq*(self: var X, statements: seq[A]) =
|
||||||
|
for statement in statements:
|
||||||
|
resolve(self, statement)
|
||||||
|
|
||||||
|
method resolve(self: var X, stmt: B) =
|
||||||
|
echo "B"
|
||||||
|
|
||||||
|
method resolve(self: var X, stmt: D) =
|
||||||
|
echo "D"
|
||||||
|
|
||||||
|
method resolve(self: var X, stmt: E) =
|
||||||
|
echo "E"
|
||||||
|
|
||||||
|
method resolve(self: var X, stmt: C) =
|
||||||
|
echo "C"
|
||||||
|
|
||||||
|
method resolve(self: var X, stmt: F) =
|
||||||
|
echo "F"
|
||||||
|
|
||||||
|
var x = X()
|
||||||
|
var a = @[A(), B(), C(), D(), E(), F()]
|
||||||
|
resolveSeq(x, a)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue