Add the return type of constructor (#48)

This commit is contained in:
konqoro 2016-11-23 18:41:14 +02:00 • committed by Flaviu Tamas
commit a04d029226

View file

@ -162,10 +162,13 @@ macro class*(head, body: untyped): untyped =
case node.kind: case node.kind:
of nnkMethodDef, nnkProcDef: of nnkMethodDef, nnkProcDef:
# make sure it is not the constructor # check if it is not the ctor proc
if node.name.basename != ctorName: if node.name.basename != ctorName:
# inject `this: T` into the arguments # inject `this: T` into the arguments
node.params.insert(1, newIdentDefs(ident("this"), typeName)) node.params.insert(1, newIdentDefs(ident("this"), typeName))
else:
# specify the return type of the ctor proc
node.params[0] = typeName
result.add(node) result.add(node)
of nnkVarSection: of nnkVarSection:
@ -236,7 +239,7 @@ class Cat of Animal:
method vocalize: string = "meow" method vocalize: string = "meow"
class Rabbit of Animal: class Rabbit of Animal:
proc newRabbit(name: string, age: int): Rabbit = proc newRabbit(name: string, age: int) = # the constructor doesn't need a return type
result = Rabbit(name: name, age: age) result = Rabbit(name: name, age: age)
method vocalize: string = "meep" method vocalize: string = "meep"