Here you go:
    
    
    type
      Shape = ref object of RootObj
      
      Rect = ref object of Shape
        width:float
        height:float
      
      ConvexP = ref object of Shape
        vertices:seq[float]
    
    type Drawing = object   # Don't use ref object if there is no need, this 
involves the GC and is inefficient
      shape: Shape
    
    method display_type_of(self: Shape) {.base.} =
      raise newException(ValueError, "Please implement display_type_of for your 
custom shape")
    
    method display_type_of(self:Rect) =
      echo "This is a rectangle "
    
    method display_type_of(self:ConvexP) =
      echo "This is a convex polygon "
    
    let rect = Rect(width: 2.0, height:2.0)
    
    var drawing = Drawing()
    
    drawing.shape = rect
    
    display_type_of drawing.shape # This is a rectangle
    
    
    Run

Reply via email to