Add the return type of constructor (#48)
This commit is contained in:
parent
587098c158
commit
a04d029226
1 changed files with 5 additions and 2 deletions
|
|
@ -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"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue