From f9fc2995a9e33ac5633890d6296db6fe87d2b292 Mon Sep 17 00:00:00 2001 From: Joey Date: Sun, 14 Jun 2015 10:46:01 -0600 Subject: [PATCH] Made it so that `new` works instead of `new_class`. --- oop_macro.nim | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/oop_macro.nim b/oop_macro.nim index 33e51ef..1c2c80b 100644 --- a/oop_macro.nim +++ b/oop_macro.nim @@ -3,14 +3,21 @@ import strutils {.hint[XDeclaredButNotUsed]: off.} -macro new_class*(obj: untyped): untyped= +macro new*(obj: untyped): untyped= ## Creates a new instance of the class ## and calls the init() method on it - quote do: - var init_obj = `obj` - when compiles(init_obj.init()): - init_obj.init() - init_obj + if obj.kind == nnkObjConstr: + # Only if the node is an object constructor + result = quote do: + var init_obj = `obj` + when compiles(init_obj.init()): + init_obj.init() + init_obj + else: + # Otherwise, just revert to system.new + result = quote do: + var init_obj = `obj` + system.new(init_obj) macro class*(head: untyped, body: untyped): untyped= @@ -281,9 +288,9 @@ class Tiger of Cat: if isMainModule: var animals: seq[Animal] = @[] - animals.add(new_class(Dog(name: "Sparky", age: 10))) - animals.add(new_class(Cat(name: "Mitten", age: 10))) - animals.add(new_class(Tiger(name: "Jean", age: 2))) + animals.add(new Dog(name: "Sparky", age: 10)) + animals.add(new Cat(name: "Mitten", age: 10)) + animals.add(new Tiger(name: "Jean", age: 2)) for a in animals: echo a.name, " says ", a.vocalize()