I would like to translate this C++ code into Nim
    
    
    struct Foo {
      int i;
    };
    struct Bar : Foo {
      int j;
    };
    

The most obvious way I can see is 
    
    
    type
      Foo {.inheritable} = object
        i: int
      
      Bar = object of Foo
        j: int
    

Which works fine, but adds a pointer to the beginning of the resulting C++ truct
    
    
    # generated C++, simplified
    struct Foo {
      TNimType* m_type
      int i;
    };
    struct Bar : Foo {
      int j;
    };
    
    

This doesn't work for my use-case, because the library I am working with 
provides me with a memory blob and requires me cast it to my type. So I need to 
replicate the C++ type exactly, without extra bytes at the beginning.

If I was providing the blob _to_ the library, I could increase the pointer by 
sizeof[TNimType] bytes and it would work. But because I am _receiving_ the 
blob, I have no control over the memory location, so I can 't add a TNimType 
pointer to the beginning without copying the whole thing somewhere else to make 
room at the beginning.

So I need to recreate the type in Nim in a manageable way.

For now, I just used the magic of copy and paste, which works fine, but will 
become tedious rather quickly (the real Foo is loooong and gets used in lots of 
different places).
    
    
    struct Bar {
      int i;
      int j;
    };
    

I would love to just replicate the inheritence syntax without the extra 
pointer, but I have no idea where to start- template? macro?
    
    
    type
      Foo = object
        i: int
      Bar = object MyNewOp Foo
        j: int
    
    

Help much appreciated! 

Reply via email to