My last 2 "solutions" don't work either because for the first one I'd need a 
proper nominal abstraction feature(or how to store a concept in a seq when the 
elements can be different) and for the second one I'd need existential types(or 
how to tell the that a seq can hold a HeterogenCell with any type parameter 
when the cell's parameter type is explicitly restricted to a certain concept).

I'd an idea but it's failing for some reason:
    
    
    type
      Packet = ref object of RootObj
        data: string
      HgPackType* = concept self
        pack(Packet, self)
      HgCell[T, S] = object of RootObj
        data: T
        handler: proc(dat: T, buffer: S): void
      HgPack[S] = object of RootObj
        buffer: S
        cells: seq[HgCell[RootObj, S]]
    
    method pack*(p: Packet, i: int8) =
      p.data &= $i
    
    proc newHgPack*[S](): auto = HgPack[S](cells: @[])
    method register*[S, T: HgPackType](msgp: HgPack[S], elem: T, handler: 
proc(dat: T, b: S): void) =
      msgp.add(HgCell(elem, handler))
    
    let mp = newHgPack[Packet]()
    mp.register(int8(1), proc(i: int8, buff: Packet) = buff.pack(i))
    
    
    Run

Reply via email to