summary refs log tree commit diff stats
path: root/tests/cpp/tmember_forward_declaration.nim
blob: 2f4a79daa393aa646811269106211599d8b61145 (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
discard """
  targets: "cpp"
  cmd: "nim cpp $file"
  output: '''
abc called
def called
abc called
'''
"""

type Foo = object

proc abc(this: Foo, x: int): void {.member: "$1('2 #2)".}
proc def(this: Foo, y: int): void {.virtual: "$1('2 #2)".}

proc abc(this: Foo, x: int): void =
  echo "abc called"
  if x > 0:
    this.def(x - 1)

proc def(this: Foo, y: int): void =
  echo "def called"
  this.abc(y)

var x = Foo()
x.abc(1)
"s2">""" # Test first class iterator: import strutils iterator tokenize2(s: string, seps: set[char] = Whitespace): tuple[ token: string, isSep: bool] {.closure.} = var i = 0 while i < s.len: var j = i if s[j] in seps: while j < s.len and s[j] in seps: inc(j) if j > i: yield (substr(s, i, j-1), true) else: while j < s.len and s[j] notin seps: inc(j) if j > i: yield (substr(s, i, j-1), false) i = j iterator count3(): int {.closure.} = yield 1 yield 2 yield 3 for word, isSep in tokenize2("ta da", WhiteSpace): if not isSep: stdout.write(word) echo "" proc inProc() = for c in count3(): echo c for word, isSep in tokenize2("ta da", WhiteSpace): stdout.write(word) for c in count3(): for d in count3(): echo c, " ", d inProc() iterator count0(): int {.closure.} = # note: doesn't require anything in its closure (except 'state') yield 0 iterator count2(): int {.closure.} = # note: requires 'x' in its closure var x = 1 yield x inc x yield x # a first class iterator has the type 'proc {.closure.}', but maybe # it shouldn't: proc invoke(iter: iterator(): int {.closure.}) = for x in iter(): echo x invoke(count0) invoke(count2) # simple tasking: type TTask = iterator (ticker: int) iterator a1(ticker: int) {.closure.} = echo "a1: A" yield echo "a1: B" yield echo "a1: C" yield echo "a1: D" iterator a2(ticker: int) {.closure.} = echo "a2: A" yield echo "a2: B" yield echo "a2: C" proc runTasks(t: varargs[TTask]) = var ticker = 0 while true: let x = t[ticker mod t.len] if finished(x): break x(ticker) inc ticker runTasks(a1, a2)