This thread is a continuation of <https://forum.nim-lang.org/t/12042>

In response to ElegantBeef, I have taken creative liberties of my own to make 
the code even better ;)
    
    
    type
      Base* = object of RootObj
        name*: string
        age*: uint
        job*: string
      
      BaseExt* = object of Base
        skills*: seq[string]
    
    proc `setBase=`(o: var BaseExt, b: Base) =
      for child, parent in Base(o).fields(b):
        child = parent
    
    proc `+`(o: BaseExt, b: Base): BaseExt =
      var newObj = o
      newObj.setBase = b
      result = newObj
    
    proc `+`(b: Base, o: BaseExt): BaseExt =
      var newObj = o
      newObj.setBase = b
      result = newObj
    
    let base = Base(name: "John Doe", age: 2000, job: "N/A")
    let baseExt = BaseExt(skills: @["baking", "potato"]) + base
    let baseExt2 = base + BaseExt(skills: @["baking", "potato"])
    
    assert baseExt == baseExt2
    
    echo "baseExt:  ", baseExt
    echo "baseExt2: ", baseExt2
    
    # baseExt:  (skills: @["baking", "potato"], name: "John Doe", age: 2000, 
job: "N/A")
    # baseExt2: (skills: @["baking", "potato"], name: "John Doe", age: 2000, 
job: "N/A")
    
    
    
    Run

If reviving this thread is against a mods wishes then I apologize, I only felt 
that the discussion as it pertained to programming was interesting and that 
maybe we could challenge each other to make this code as elegant as possible :)

Reply via email to