File: typeclassborrow.nim

package info (click to toggle)
nim 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,911,644 kB
  • sloc: sh: 24,603; ansic: 1,761; python: 1,492; makefile: 1,013; sql: 298; asm: 141; xml: 13
file content (56 lines) | stat: -rw-r--r-- 1,056 bytes parent folder | download
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
import std/tables

type
  Foo = distinct seq[int]
  Bar[N: static[int]] = distinct seq[int]
  Baz = distinct Bar[10]

proc newSeq(s: var Foo, n: Natural) {.borrow.}
proc newSeq(s: var Bar, n: Natural) {.borrow.}
proc newSeq(s: var Baz, n: Natural) {.borrow.}


proc `$`(s: Foo): string {.borrow.}
proc `$`(s: Bar): string {.borrow.}
proc `$`(s: Baz): string {.borrow.}

proc doThing(b: Bar) = discard
proc doThing(b: Baz) {.borrow.}

var
  foo: Foo
  bar: Bar[10]
  baz: Baz

newSeq(foo, 100)
newSeq(bar, bar.N)
newSeq(baz, 10)

bar.doThing()
baz.doThing()

assert $seq[int](foo) == $foo
assert $seq[int](bar) == $bar
assert $seq[int](baz) == $baz

type
  Fine* = distinct string

proc `==`*(x, y: Fine): bool {.borrow.} =
  ## Here is the documentation
  runnableExamples:
    var x = Fine("1234")
    var y = Fine("1234")
    doAssert x == y
  doAssert false


var x = Fine("1234")
var y = Fine("1234")
doAssert x == y

block: # bug #22902
  type
    DistinctTable = distinct Table[int, int]

  proc `[]`(t: DistinctTable; key: int): lent int {.borrow.}