type
TCurve2d* =object of TObject
tag* :int
TLine2d* =object of TCurve2d
x1,y1,x2,y2:float
proc createLine(x1,y1,x2,y2:float):TLine2d=
result.x1=x1
result.y1=y1
result.x2=x2
result.y2=y2
method toString(curve:TCurve2d):string=
return "Abstract toString called"
method toString(line:TLine2d):string=
return $line.x1 & "," & $line.y1 & "," & $line.x2 & "," & $line.y2
let line1:TLine2d=createLine(1,2,3,4)
let line2:TCurve2d=createLine(1,2,3,4)
echo toString(line1) #prints 1,2,3,4 as expected
echo toString(line2) #BUG (?)=> prints 0,0,0,0 (or some other random stuff)
Araq: That said, inheritance really works much better with ref object anyway.
I'd go a step further and say that inheritance for non-ref objects is essentially useless. In fact, I'd even suggest removing it from the language entirely to avoid confusion if not for backwards compatibility.
The reason is that the purpose of inheritance is to enable polymorphism. But you cannot actually have polymorphic variables of an object type, since every assignment to an object variable will be silently coerced to the declared type by stripping of any extraneous fields. In practice, this results in much the same problems that converters can have, only worse.
That's not to say that the issue can't be resolved at all, but you'd essentially need a mechanism to ensure that subclasses can share a common layout with their parent classes.