Alternatively, one could use a generic constructor: # lib.nim import typetraits type Element* = ref object of RootObj id: string proc newElement*(id: string, T: typedesc[Element] = Element): T = result.new result.id = id echo "Instantiated an object of type ", T.name, ", size: ", result[].sizeOf proc printId*(e: Element) = echo e.id # user.nim import lib type ElementA = ref object of Element n: int proc newElementA(id: string, n: int): ElementA = result = newElement(id, ElementA) result.n = n let e = newElement("base") ea = newElementA("A", 5) e.printId ea.printId echo ea.n Run
- How to immutably initialize parent object with private fie... bluenote
- Re: How to immutably initialize parent object with pr... Stefan_Salewski
- Re: How to immutably initialize parent object wit... bluenote
- Re: How to immutably initialize parent object... lscrd
- Re: How to immutably initialize parent ob... bluenote
- Re: How to immutably initialize pare... mratsim
- Re: How to immutably initialize ... lscrd
- Re: How to immutably initial... bluenote
- Re: How to immutably initial... gemath
- Re: How to immutably initial... Rania_d
- Re: How to immutably initial... bluenote
- Re: How to immutably initial... Araq
- Re: How to immutably initial... bluenote