summary refs log tree commit diff stats
path: root/tests/overload/toverload_various.nim
blob: c4f8ecbf8155c21e7f0ace1b82f7c02e41e0de40 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
discard """
  output: '''
true012innertrue
m1
tup1
another number: 123
yay
helloa 1 b 2 x @[3, 4, 5] y 6 z 7
yay
12
ref ref T ptr S
dynamic: let
dynamic: var
static: const
static: literal
static: constant folding
static: static string
foo1
'''
"""


import strutils, sequtils


block overl2:
  # Test new overloading resolution rules
  proc toverl2(x: int): string = return $x
  proc toverl2(x: bool): string = return $x

  iterator toverl2(x: int): int =
    var res = 0
    while res < x:
      yield res
      inc(res)

  var
    pp: proc (x: bool): string {.nimcall.} = toverl2

  stdout.write(pp(true))

  for x in toverl2(3):
    stdout.write(toverl2(x))

  block:
    proc toverl2(x: int): string = return "inner"
    stdout.write(toverl2(5))
    stdout.write(true)

  stdout.write("\n")
  #OUT true012innertrue



block overl3:
  # Tests more specific generic match:
  proc m[T](x: T) = echo "m2"
  proc m[T](x: var ref T) = echo "m1"
  proc tup[S, T](x: tuple[a: S, b: ref T]) = echo "tup1"
  proc tup[S, T](x: tuple[a: S, b: T]) = echo "tup2"

  var
    obj: ref int
    tu: tuple[a: int, b: ref bool]

  m(obj)
  tup(tu)



block toverprc:
  # Test overloading of procs when used as function pointers
  proc parseInt(x: float): int {.noSideEffect.} = discard
  proc parseInt(x: bool): int {.noSideEffect.} = discard
  proc parseInt(x: float32): int {.noSideEffect.} = discard
  proc parseInt(x: int8): int {.noSideEffect.} = discard
  proc parseInt(x: File): int {.noSideEffect.} = discard
  proc parseInt(x: char): int {.noSideEffect.} = discard
  proc parseInt(x: int16): int {.noSideEffect.} = discard

  proc parseInt[T](x: T): int = echo x; 34

  type
    TParseInt = proc (x: string): int {.noSideEffect.}

  var
    q = TParseInt(parseInt)
    p: TParseInt = parseInt

  proc takeParseInt(x: proc (y: string): int {.noSideEffect.}): int =
    result = x("123")

  if false:
    echo "Give a list of numbers (separated by spaces): "
    var x = stdin.readline.split.map(parseInt).max
    echo x, " is the maximum!"
  echo "another number: ", takeParseInt(parseInt)


  type
    TFoo[a,b] = object
      lorem: a
      ipsum: b

  proc bar[a,b](f: TFoo[a,b], x: a) = echo(x, " ", f.lorem, f.ipsum)
  proc bar[a,b](f: TFoo[a,b], x: b) = echo(x, " ", f.lorem, f.ipsum)

  discard parseInt[string]("yay")



block toverwr:
  # Test the overloading resolution in connection with a qualifier
  proc write(t: File, s: string) =
    discard # a nop
  system.write(stdout, "hello")
  #OUT hello



block tparams_after_varargs:
  proc test(a, b: int, x: varargs[int]; y, z: int) =
    echo "a ", a, " b ", b, " x ", @x, " y ", y, " z ", z

  test 1, 2, 3, 4, 5, 6, 7

  # XXX maybe this should also work with ``varargs[untyped]``
  template takesBlockA(a, b: untyped; x: varargs[typed]; blck: untyped): untyped =
    blck
    echo a, b

  takesBlockA 1, 2, "some", 0.90, "random stuff":
    echo "yay"



block tprefer_specialized_generic:
  proc foo[T](x: T) =
    echo "only T"

  proc foo[T](x: ref T) =
    echo "ref T"

  proc foo[T, S](x: ref ref T; y: ptr S) =
    echo "ref ref T ptr S"

  proc foo[T, S](x: ref T; y: ptr S) =
    echo "ref T ptr S"

  proc foo[T](x: ref T; default = 0) =
    echo "ref T; default"

  var x: ref ref int
  var y: ptr ptr int
  foo(x, y)



block tstaticoverload:
  proc foo(s: string) =
    echo "dynamic: ", s

  proc foo(s: static[string]) =
    echo "static: ", s

  let l = "let"
  var v = "var"
  const c = "const"

  type staticString = static[string]

  foo(l)
  foo(v)
  foo(c)
  foo("literal")
  foo("constant" & " " & "folding")
  foo(staticString("static string"))


# bug #6076

type A[T] = object

proc regr(a: A[void]) = echo "foo1"
proc regr[T](a: A[T]) = doAssert(false)

regr(A[void]())


type Foo[T] = object

proc regr[T](p: Foo[T]): seq[T] =
  discard

proc regr(p: Foo[void]): seq[int] =
  discard


discard regr(Foo[int]())
discard regr(Foo[void]())