toverload_various.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. discard """
  2. output: '''
  3. true012innertrue
  4. m1
  5. tup1
  6. another number: 123
  7. yay
  8. helloa 1 b 2 x @[3, 4, 5] y 6 z 7
  9. yay
  10. 12
  11. ref ref T ptr S
  12. dynamic: let
  13. dynamic: var
  14. static: const
  15. static: literal
  16. static: constant folding
  17. static: static string
  18. '''
  19. """
  20. import strutils, sequtils
  21. block overl2:
  22. # Test new overloading resolution rules
  23. proc toverl2(x: int): string = return $x
  24. proc toverl2(x: bool): string = return $x
  25. iterator toverl2(x: int): int =
  26. var res = 0
  27. while res < x:
  28. yield res
  29. inc(res)
  30. var
  31. pp: proc (x: bool): string {.nimcall.} = toverl2
  32. stdout.write(pp(true))
  33. for x in toverl2(3):
  34. stdout.write(toverl2(x))
  35. block:
  36. proc toverl2(x: int): string = return "inner"
  37. stdout.write(toverl2(5))
  38. stdout.write(true)
  39. stdout.write("\n")
  40. #OUT true012innertrue
  41. block overl3:
  42. # Tests more specific generic match:
  43. proc m[T](x: T) = echo "m2"
  44. proc m[T](x: var ref T) = echo "m1"
  45. proc tup[S, T](x: tuple[a: S, b: ref T]) = echo "tup1"
  46. proc tup[S, T](x: tuple[a: S, b: T]) = echo "tup2"
  47. var
  48. obj: ref int
  49. tu: tuple[a: int, b: ref bool]
  50. m(obj)
  51. tup(tu)
  52. block toverprc:
  53. # Test overloading of procs when used as function pointers
  54. proc parseInt(x: float): int {.noSideEffect.} = discard
  55. proc parseInt(x: bool): int {.noSideEffect.} = discard
  56. proc parseInt(x: float32): int {.noSideEffect.} = discard
  57. proc parseInt(x: int8): int {.noSideEffect.} = discard
  58. proc parseInt(x: File): int {.noSideEffect.} = discard
  59. proc parseInt(x: char): int {.noSideEffect.} = discard
  60. proc parseInt(x: int16): int {.noSideEffect.} = discard
  61. proc parseInt[T](x: T): int = echo x; 34
  62. type
  63. TParseInt = proc (x: string): int {.noSideEffect.}
  64. var
  65. q = TParseInt(parseInt)
  66. p: TParseInt = parseInt
  67. proc takeParseInt(x: proc (y: string): int {.noSideEffect.}): int =
  68. result = x("123")
  69. if false:
  70. echo "Give a list of numbers (separated by spaces): "
  71. var x = stdin.readline.split.map(parseInt).max
  72. echo x, " is the maximum!"
  73. echo "another number: ", takeParseInt(parseInt)
  74. type
  75. TFoo[a,b] = object
  76. lorem: a
  77. ipsum: b
  78. proc bar[a,b](f: TFoo[a,b], x: a) = echo(x, " ", f.lorem, f.ipsum)
  79. proc bar[a,b](f: TFoo[a,b], x: b) = echo(x, " ", f.lorem, f.ipsum)
  80. discard parseInt[string]("yay")
  81. block toverwr:
  82. # Test the overloading resolution in connection with a qualifier
  83. proc write(t: File, s: string) =
  84. discard # a nop
  85. system.write(stdout, "hello")
  86. #OUT hello
  87. block tparams_after_varargs:
  88. proc test(a, b: int, x: varargs[int]; y, z: int) =
  89. echo "a ", a, " b ", b, " x ", @x, " y ", y, " z ", z
  90. test 1, 2, 3, 4, 5, 6, 7
  91. # XXX maybe this should also work with ``varargs[untyped]``
  92. template takesBlockA(a, b: untyped; x: varargs[typed]; blck: untyped): untyped =
  93. blck
  94. echo a, b
  95. takesBlockA 1, 2, "some", 0.90, "random stuff":
  96. echo "yay"
  97. block tprefer_specialized_generic:
  98. proc foo[T](x: T) =
  99. echo "only T"
  100. proc foo[T](x: ref T) =
  101. echo "ref T"
  102. proc foo[T, S](x: ref ref T; y: ptr S) =
  103. echo "ref ref T ptr S"
  104. proc foo[T, S](x: ref T; y: ptr S) =
  105. echo "ref T ptr S"
  106. proc foo[T](x: ref T; default = 0) =
  107. echo "ref T; default"
  108. var x: ref ref int
  109. var y: ptr ptr int
  110. foo(x, y)
  111. block tstaticoverload:
  112. proc foo(s: string) =
  113. echo "dynamic: ", s
  114. proc foo(s: static[string]) =
  115. echo "static: ", s
  116. let l = "let"
  117. var v = "var"
  118. const c = "const"
  119. type staticString = static[string]
  120. foo(l)
  121. foo(v)
  122. foo(c)
  123. foo("literal")
  124. foo("constant" & " " & "folding")
  125. foo(staticString("static string"))