Fix generic forward declarations; fixes #4104; fixes #4908 (#5566)

This commit is contained in:
zah 2017-03-23 13:40:57 +02:00 • committed by Andreas Rumpf
commit be174fc3c7
5 changed files with 36 additions and 15 deletions

View file

@ -1,5 +1,6 @@
discard """
output: '''b()
720 120.0
720 120.0'''
"""
@ -16,13 +17,12 @@ proc fac[T](x: T): T =
echo fac(6), " ", fac(5.0)
when false:
# This still doesn't work...
# test recursive generic with forwarding:
proc fac2[T](x: T): T
# test recursive generic with forwarding:
proc fac2[T](x: T): T
echo fac2(6), " ", fac2(5.0)
echo fac2(6), " ", fac2(5.0)
proc fac2[T](x: T): T =
if x == 0: return 1
else: return fac2(x-1)*x
proc fac2[T](x: T): T =
if x == 0: return 1
else: return fac2(x-1)*x

View file

@ -1,7 +1,6 @@
discard """
output: "1.1000000000000001e+00 11"
output: "1.1 11\n42\n0"
ccodecheck: "!@'ClEnv'"
disabled: "true"
"""
proc p[T](a, b: T): T
@ -12,3 +11,18 @@ proc p[T](a, b: T): T =
let c = b
result = a + b + c
# https://github.com/nim-lang/Nim/issues/4908
proc foo(t: typedesc[int]): int
proc bar(): int = foo(int)
proc foo(t: typedesc[int]): int =
return 0
# https://github.com/nim-lang/Nim/issues/4104
proc print[T](t: T) # Error: implementation of 'print.print(t: int)' expected
print 42 # moving this line after the implementation fixes the error,
# but such behaviour makes forward declaration pointless
proc print[T](t: T) =
echo t
echo bar()