summary refs log tree commit diff stats
path: root/tests/distinct/tdistinct.nim
blob: b6ba7aa9935105eab745ef0157d3089d18234faf (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
discard """
  targets: "c js"
  output: '''
tdistinct
25
false
false
false
false
Foo
foo
'''
"""

echo "tdistinct"

block tborrowdot:
  type
    Foo = object
      a, b: int
      s: string

    Bar {.borrow: `.`.} = distinct Foo

  var bb: ref Bar
  new bb
  bb.a = 90
  bb.s = "abc"

block tcurrncy:
  template Additive(typ: untyped) =
    proc `+`(x, y: typ): typ {.borrow.}
    proc `-`(x, y: typ): typ {.borrow.}

    # unary operators:
    proc `+`(x: typ): typ {.borrow.}
    proc `-`(x: typ): typ {.borrow.}

  template Multiplicative(typ, base: untyped) =
    proc `*`(x: typ, y: base): typ {.borrow.}
    proc `*`(x: base, y: typ): typ {.borrow.}
    proc `div`(x: typ, y: base): typ {.borrow.}
    proc `mod`(x: typ, y: base): typ {.borrow.}

  template Comparable(typ: untyped) =
    proc `<`(x, y: typ): bool {.borrow.}
    proc `<=`(x, y: typ): bool {.borrow.}
    proc `==`(x, y: typ): bool {.borrow.}

  template DefineCurrency(typ, base: untyped) =
    type
      typ = distinct base
    Additive(typ)
    Multiplicative(typ, base)
    Comparable(typ)

    proc `$`(t: typ): string {.borrow.}

  DefineCurrency(TDollar, int)
  DefineCurrency(TEuro, int)
  echo($( 12.TDollar + 13.TDollar )) #OUT 25

block tconsts:
  # bug #2641

  type MyChar = distinct char
  const c:MyChar = MyChar('a')

  type MyBool = distinct bool
  const b:MyBool = MyBool(true)

  type MyBoolSet = distinct set[bool]
  const bs:MyBoolSet = MyBoolSet({true})

  type MyCharSet= distinct set[char]
  const cs:MyCharSet = MyCharSet({'a'})

  type MyBoolSeq = distinct seq[bool]
  const bseq:MyBoolSeq = MyBoolSeq(@[true, false])

  type MyBoolArr = distinct array[3, bool]
  const barr:MyBoolArr = MyBoolArr([true, false, true])

# bug #2760

type
  DistTup = distinct tuple
    foo, bar: string

const d: DistTup = DistTup((
  foo:"FOO", bar:"BAR"
))


# bug #7167

type Id = distinct range[0..3]

proc `<=`(a, b: Id): bool {.borrow.}

var xs: array[Id, bool]

for x in xs: echo x # type mismatch: got (T) but expected 'bool'

# bug #11715

type FooD = distinct int
proc `<=`(a, b: FooD): bool {.borrow.}

for f in [FooD(0): "Foo"]: echo f

block tRequiresInit:
  template accept(x) =
    static: doAssert compiles(x)

  template reject(x) =
    static: doAssert not compiles(x)

  type
    Foo = object
      x: string

    DistinctFoo {.requiresInit, borrow: `.`.} = distinct Foo
    DistinctString {.requiresInit.} = distinct string

  reject:
    var foo: DistinctFoo
    foo.x = "test"
    doAssert foo.x == "test"

  accept:
    let foo = DistinctFoo(Foo(x: "test"))
    doAssert foo.x == "test"

  reject:
    var s: DistinctString
    s = "test"
    doAssert string(s) == "test"

  accept:
    let s = DistinctString("test")
    doAssert string(s) == "test"

block: #17322
  type
    A[T] = distinct string

  proc foo(a: var A) =
    a.string.add "foo"

  type
    B = distinct A[int]

  var b: B
  foo(A[int](b))
  echo A[int](b).string
  b.string.add "bar"
  assert b.string == "foobar"

type Foo = distinct string

proc main() = # proc instead of template because of MCS/UFCS.
  # xxx put everything here to test under RT + VM
  block: # bug #12282
    block:
      proc test() =
        var s: Foo
        s.string.add('c')
        doAssert s.string == "c" # was failing
      test()

    block:
      proc add(a: var Foo, b: char) {.borrow.}
      proc test() =
        var s: Foo
        s.add('c')
        doAssert s.string == "c" # was ok
      test()

    block:
      proc add(a: var Foo, b: char) {.borrow.}
      proc test() =
        var s: string
        s.Foo.add('c')
        doAssert s.string == "c" # was failing
      test()
    block: #18061
      type
        A = distinct (0..100)
        B = A(0) .. A(10)
      proc test(b: B) = discard
      let
        a = A(10)
        b = B(a)
      test(b)

      proc test(a: A) = discard
      discard cast[B](A(1))
      var c: B


  block: # bug #9423
    block:
      type Foo = seq[int]
      type Foo2 = distinct Foo
      template fn() =
        var a = Foo2(@[1])
        a.Foo.add 2
        doAssert a.Foo == @[1, 2]
      fn()

    block:
      type Stack[T] = distinct seq[T]
      proc newStack[T](): Stack[T] =
        Stack[T](newSeq[T]())
      proc push[T](stack: var Stack[T], elem: T) =
        seq[T](stack).add(elem)
      proc len[T](stack: Stack[T]): int =
        seq[T](stack).len
      proc fn() = 
        var stack = newStack[int]()
        stack.push(5)
        doAssert stack.len == 1
      fn()

static: main()
main()