From 20b5f31c03fb556ec0aa2428a40adbac004d8987 Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 13 Jan 2014 02:10:03 +0100 Subject: new tester; all tests categorized --- tests/method/tmultim1.nim | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/method/tmultim1.nim (limited to 'tests/method/tmultim1.nim') diff --git a/tests/method/tmultim1.nim b/tests/method/tmultim1.nim new file mode 100644 index 000000000..7f551aa64 --- /dev/null +++ b/tests/method/tmultim1.nim @@ -0,0 +1,29 @@ +discard """ + file: "tmultim1.nim" + output: "7" +""" +# Test multi methods + +type + Expression = ref object {.inheritable.} + Literal = ref object of Expression + x: int + PlusExpr = ref object of Expression + a, b: Expression + +method eval(e: Expression): int = quit "to override!" +method eval(e: Literal): int = return e.x +method eval(e: PlusExpr): int = return eval(e.a) + eval(e.b) + +proc newLit(x: int): Literal = + new(result) + result.x = x + +proc newPlus(a, b: Expression): PlusExpr = + new(result) + result.a = a + result.b = b + +echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4))) #OUT 7 + + -- cgit 1.4.1-2-gfad0