Made it so that new works instead of new_class.
This commit is contained in:
parent
321be06f40
commit
f9fc2995a9
1 changed files with 16 additions and 9 deletions
|
|
@ -3,14 +3,21 @@ import strutils
|
||||||
|
|
||||||
{.hint[XDeclaredButNotUsed]: off.}
|
{.hint[XDeclaredButNotUsed]: off.}
|
||||||
|
|
||||||
macro new_class*(obj: untyped): untyped=
|
macro new*(obj: untyped): untyped=
|
||||||
## Creates a new instance of the class
|
## Creates a new instance of the class
|
||||||
## and calls the init() method on it
|
## and calls the init() method on it
|
||||||
quote do:
|
if obj.kind == nnkObjConstr:
|
||||||
var init_obj = `obj`
|
# Only if the node is an object constructor
|
||||||
when compiles(init_obj.init()):
|
result = quote do:
|
||||||
init_obj.init()
|
var init_obj = `obj`
|
||||||
init_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=
|
macro class*(head: untyped, body: untyped): untyped=
|
||||||
|
|
@ -281,9 +288,9 @@ class Tiger of Cat:
|
||||||
|
|
||||||
if isMainModule:
|
if isMainModule:
|
||||||
var animals: seq[Animal] = @[]
|
var animals: seq[Animal] = @[]
|
||||||
animals.add(new_class(Dog(name: "Sparky", age: 10)))
|
animals.add(new Dog(name: "Sparky", age: 10))
|
||||||
animals.add(new_class(Cat(name: "Mitten", age: 10)))
|
animals.add(new Cat(name: "Mitten", age: 10))
|
||||||
animals.add(new_class(Tiger(name: "Jean", age: 2)))
|
animals.add(new Tiger(name: "Jean", age: 2))
|
||||||
|
|
||||||
for a in animals:
|
for a in animals:
|
||||||
echo a.name, " says ", a.vocalize()
|
echo a.name, " says ", a.vocalize()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue