Sorry for that basic topic but I want an object field to be able to handle two
differents types with a clean syntax on the user side. No problem if the code
behinds the scene is a hacky template or something, but the user should never
need to see that.
I written a simplified example.
type
Rect = ref object
width:float
height:float
ConvexP = ref object
vertices:seq[float]
type Drawing = ref object
shape:Shape # I want this to hold a Rect or a ConvexP
# two stupid functions who just show if the program works.
proc presents(rect:Rect) =
echo "This is a rectangle "
proc presents(convP:ConvexP) =
echo "This is a convex polygon "
var rect = Rect(width: 2.0, height:2.0)
var drawing = Drawing()
drawing.shape = rect
drawing.presents shape # must runs the first version of "display"
Run
This code is wrong because Shape is still not defined.
I want to avoid generics parameters because the code will be cluttered by many
"[]", "Drawing" may handle several generic fields in the future, not only the
shape. What is your suggestion ?