commit
2f9ad55fbd
3 changed files with 39 additions and 1 deletions
|
|
@ -158,11 +158,15 @@ proc sumGeneric(t: PType): int =
|
||||||
t = t.sons[0]
|
t = t.sons[0]
|
||||||
inc result
|
inc result
|
||||||
inc isvar
|
inc isvar
|
||||||
|
of tyTypeDesc:
|
||||||
|
t = t.lastSon
|
||||||
|
if t.kind == tyEmpty: break
|
||||||
|
inc result
|
||||||
of tyGenericInvocation, tyTuple:
|
of tyGenericInvocation, tyTuple:
|
||||||
result += ord(t.kind == tyGenericInvocation)
|
result += ord(t.kind == tyGenericInvocation)
|
||||||
for i in 0 .. <t.len: result += t.sons[i].sumGeneric
|
for i in 0 .. <t.len: result += t.sons[i].sumGeneric
|
||||||
break
|
break
|
||||||
of tyGenericParam, tyExpr, tyStatic, tyStmt, tyTypeDesc: break
|
of tyGenericParam, tyExpr, tyStatic, tyStmt: break
|
||||||
of tyBool, tyChar, tyEnum, tyObject, tyProc, tyPointer,
|
of tyBool, tyChar, tyEnum, tyObject, tyProc, tyPointer,
|
||||||
tyString, tyCString, tyInt..tyInt64, tyFloat..tyFloat128,
|
tyString, tyCString, tyInt..tyInt64, tyFloat..tyFloat128,
|
||||||
tyUInt..tyUInt64:
|
tyUInt..tyUInt64:
|
||||||
|
|
|
||||||
11
tests/generics/mgenericprocmatcher.nim
Normal file
11
tests/generics/mgenericprocmatcher.nim
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import tables
|
||||||
|
|
||||||
|
type
|
||||||
|
FFoo* = object
|
||||||
|
FBar* = object
|
||||||
|
|
||||||
|
proc new*(_: typedesc[FFoo]): int = 2
|
||||||
|
proc new*[T](_: typedesc[T]): int = 3
|
||||||
|
proc new*(_: typedesc): int = 4
|
||||||
|
proc new*(_: typedesc[seq[Table[int, seq[Table[int, string]]]]]): int = 5
|
||||||
|
proc new*(_: typedesc[seq[Table[int, seq[Table[int, typedesc]]]]]): int = 6
|
||||||
23
tests/generics/tgenericprocmatcher.nim
Normal file
23
tests/generics/tgenericprocmatcher.nim
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
discard """
|
||||||
|
exitcode: 0
|
||||||
|
"""
|
||||||
|
import mgenericprocmatcher
|
||||||
|
import tables
|
||||||
|
|
||||||
|
type
|
||||||
|
LFoo = object
|
||||||
|
LBar = object
|
||||||
|
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
doAssert FBar.new() == 3
|
||||||
|
|
||||||
|
proc new(_: typedesc[LFoo]): int = 0
|
||||||
|
proc new[T](_: typedesc[T]): int = 1
|
||||||
|
proc new*(_: typedesc[seq[Table[int, seq[Table[int, typedesc]]]]]): int = 7
|
||||||
|
|
||||||
|
doAssert LFoo.new() == 0 # Tests selecting more precise type
|
||||||
|
doAssert LBar.new() == 1 # Tests preferring function from local scope
|
||||||
|
doAssert FBar.new() == 1
|
||||||
|
doAssert FFoo.new() == 2 # Tests selecting more precise type from other module
|
||||||
|
doAssert seq[Table[int, seq[Table[int, string]]]].new() == 5 # Truly complex type test
|
||||||
Loading…
Add table
Add a link
Reference in a new issue