summary refs log tree commit diff stats
path: root/tests/overload/tspec.nim
blob: a84bac244ddabe7e7be98b91a5b4032da8c14c66 (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
discard """
  output: '''not a var
not a var
a var
B
int
T
int16
T
ref T
123
2
1
@[123, 2, 1]
Called!
merge with var
merge no var'''
"""

# Things that's even in the spec now!

proc byvar(x: var int) = echo "a var"
proc byvar(x: int) = echo "not a var"
byvar(89)

let letSym = 0
var varSym = 13

byvar(letSym)
byvar(varSym)

type
  A = object of RootObj
  B = object of A
  C = object of B

proc p(obj: A) =
  echo "A"

proc p(obj: B) =
  echo "B"

var c = C()
# not ambiguous, calls 'B', not 'A' since B is a subtype of A
# but not vice versa:
p(c)

proc pp(obj: A, obj2: B) = echo "A B"
proc pp(obj: B, obj2: A) = echo "B A"

# but this is ambiguous:
#pp(c, c)

proc takesInt(x: int) = echo "int"
proc takesInt[T](x: T) = echo "T"
proc takesInt(x: int16) = echo "int16"

takesInt(4) # "int"
var x: int32
takesInt(x) # "T"
var y: int16
takesInt(y) # "int16"
var z: range[0..4] = 0
takesInt(z) # "T"

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

var ri: ref int
gen(ri) # "ref T"


template rem(x) = discard
#proc rem[T](x: T) = discard

rem unresolvedExpression(undeclaredIdentifier)


proc takeV[T](a: varargs[T]) =
  for x in a: echo x

takeV([123, 2, 1]) # takeV's T is "int", not "array of int"
echo(@[123, 2, 1])

# bug #2600

type
  FutureBase* = ref object of RootObj ## Untyped future.

  Future*[T] = ref object of FutureBase ## Typed future.
    value: T ## Stored value

  FutureVar*[T] = distinct Future[T]

proc newFuture*[T](): Future[T] =
  new(result)

proc newFutureVar*[T](): FutureVar[T] =
  result = FutureVar[T](newFuture[T]())

proc mget*[T](future: FutureVar[T]): var T =
  Future[T](future).value

proc reset*[T](future: FutureVar[T]) =
  echo "Called!"

proc merge[T](x: Future[T]) = echo "merge no var"
proc merge[T](x: var Future[T]) = echo "merge with var"

when true:
  var foo = newFutureVar[string]()
  foo.mget() = ""
  foo.mget.add("Foobar")
  foo.reset()
  var bar = newFuture[int]()
  bar.merge # merge with var
  merge(newFuture[int]()) # merge no var