I can not remember many examples of extended objects for Nim. For OO languages 
like Ruby we can extend everything with arbitrary custom fields. For Nim 
standard types I have never seen something like
    
    
    type
      MyStr = ref object of string
        age: int
    
    var m: MyStr = newString(); m.age = 7
    

Well, composition over inheritance?

But for the high level GTK proxy objects I need the ability to extend objects 
of course, otherwise the high level wrapper makes not much sense at all. 
Currently I have all the GUI objects working fine, with procs like newButton() 
the Nim button proxy object linked with the real GUI element is created. But 
now users may want to add new fields to the elements, so I need a proc like 
initButton()? I know we have some init procs in standard lib like initTable(), 
but I think that name is more about value and reference objects.

I just tried code like this:
    
    
    type
      O = ref object of RootObj
        i: int
      
      O1 = ref object of O
        j: int
    
    proc newO(): O =
      new(result)
      result.i = 7
    
    proc initO[T](o: var T) =
      assert(o is O)
      new(o)
      o.i = 7
    
    var o = newO()
    echo o.i
    
    var o1: O1
    initO(o1)
    o1.j = 9
    echo o1.i
    echo o1.j
    
    #var o1x = initO[O1] # does not work
    
    

Well, I think that is what may be desired, so for each GUI widget XXX I can 
provide beside a newXXX() proc a initXXX() proc which accepts extended objects. 
Is there a way to reduce
    
    
    var o1: O1
    initO(o1)
    
    

into one single statement? The commented out from above (#var o1x = initO[O1]) 
does not work of course.

Reply via email to