Thanks @doofenstein... that works just fine and requires no _magic_. I like it 🙂
I also really appreciate the feedback on _Obj_ suffix.
The resulting exemplar now looks like:
type
Parent* = ref object of RootObj
parentField: int
Child* = ref object of Parent
childField: string
# Split the initialization and object creation!
proc initParent(parent: Parent, value = 123) =
parent.parentField = value
proc newParent*(value: int): Parent =
new result
result.initParent value
proc newChild*(value: string): Child =
new result
result.initParent 321
result.childField = value
var child = newChild "ABC"
echo "child parentField: ", child.parentField
echo "child childField: ", child.childField
Run
which generates the expected:
child parentField: 321
child childField: ABC
Run