summary refs log tree commit diff stats
path: root/tests/misc/tparamsindefault.nim
blob: 3fe917f2bd6d0de9d0217a626f864754bdfa2620 (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
discard """
output: '''
@[1, 2, 3]@[1, 2, 3]
a
a
1
3 is an int
2 is an int
miau is a string
f1 1 1 1
f1 2 3 3
f1 10 20 30
f2 100 100 100
f2 200 300 300
f2 300 400 400
f3 10 10 20
f3 10 15 25
true true
false true
world
typedescDefault
'''
"""

template reject(x) =
  assert(not compiles(x))

block:
  # https://github.com/nim-lang/Nim/issues/7756
  proc foo[T](x: seq[T], y: seq[T] = x) =
    echo x, y

  let a = @[1, 2, 3]
  foo(a)

block:
  # https://github.com/nim-lang/Nim/issues/1201
  proc issue1201(x: char|int = 'a') = echo x

  issue1201()
  issue1201('a')
  issue1201(1)

  # https://github.com/nim-lang/Nim/issues/7000
  proc test(a: int|string = 2) =
    when a is int:
        echo a, " is an int"
    elif a is string:
        echo a, " is a string"

  test(3) # works
  test() # works
  test("miau")

block:
  # https://github.com/nim-lang/Nim/issues/3002 and similar
  proc f1(a: int, b = a, c = b) =
    echo "f1 ", a, " ", b, " ", c

  proc f2(a: int, b = a, c: int = b) =
    echo "f2 ", a, " ", b, " ", c

  proc f3(a: int, b = a, c = a + b) =
    echo "f3 ", a, " ", b, " ", c

  f1 1
  f1(2, 3)
  f1 10, 20, 30
  100.f2
  200.f2 300
  300.f2(400)

  10.f3()
  10.f3(15)

  reject:
    # This is a type mismatch error:
    proc f4(a: int, b = a, c: float = b) = discard

  reject:
    # undeclared identifier
    proc f5(a: int, b = c, c = 10) = discard

  reject:
    # undeclared identifier
    proc f6(a: int, b = b) = discard

  reject:
    # undeclared identifier
    proc f7(a = a) = discard

block:
  proc f(a: var int, b: ptr int, c = addr(a)) =
    echo addr(a) == b, " ",  b == c

  var x = 10
  f(x, addr(x))
  f(x, nil, nil)

block:
  # https://github.com/nim-lang/Nim/issues/1046
  proc pySubstr(s: string, start: int, endd = s.len()): string =
    var
      revStart = start
      revEnd = endd

    if start < 0:
      revStart = s.len() + start
    if endd < 0:
      revEnd = s.len() + endd

    return s[revStart ..  revEnd-1]

  echo pySubstr("Hello world", -5)


# bug #11660

func typedescDefault(T: typedesc; arg: T = 0) = debugEcho "typedescDefault"
typedescDefault(int)