From f191059e56ccf8accf872f4fb10986418dac45e2 Mon Sep 17 00:00:00 2001 From: Araq Date: Tue, 19 Jun 2012 22:37:00 +0200 Subject: somewhat working closures --- tests/reject/tinvalidclosure.nim | 7 +++++++ tests/run/tclosure2.nim | 34 ++++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 tests/reject/tinvalidclosure.nim (limited to 'tests') diff --git a/tests/reject/tinvalidclosure.nim b/tests/reject/tinvalidclosure.nim new file mode 100644 index 000000000..f5ac3a5f0 --- /dev/null +++ b/tests/reject/tinvalidclosure.nim @@ -0,0 +1,7 @@ +discard """ + line: 6 + errormsg: "'ugh' cannot have 'closure' calling convention" +""" + +proc ugh[T](x: T) {.closure.} = + echo "ugha" diff --git a/tests/run/tclosure2.nim b/tests/run/tclosure2.nim index 5a1cb8075..113c98250 100644 --- a/tests/run/tclosure2.nim +++ b/tests/run/tclosure2.nim @@ -1,20 +1,30 @@ discard """ - output: '''1 + output: '''0 +11 +1 +11 2 +11 3 +11 4 +11 5 +11 6 +11 7 +11 8 -9 -10 11 +9 11 py py py -py''' +py +px +6''' """ when true: @@ -34,7 +44,7 @@ when true: ax() -when false: +when true: proc accumulator(start: int): (proc(): int {.closure.}) = var x = start-1 #let dummy = proc = @@ -62,8 +72,14 @@ when false: outer() -when false: - proc outer = +when true: + proc outer2 = + var errorValue = 3 + proc fac[T](n: T): T = + if n < 0: result = errorValue + elif n <= 1: result = 1 + else: result = n * fac(n-1) + proc px() {.closure.} = echo "px" @@ -76,7 +92,9 @@ when false: "xyz": py } mapping[0][1]() + + echo fac(3) - outer() + outer2() -- cgit 1.4.1-2-gfad0