The question is not about interfacing with other languages, but exceptionally 
about interfacing of Nim modules with each other. The C language allows to hide 
internals of a type declaration but still have type checking using just 
"half-anonymous" `struct BoxOfStuff *` pointer. I want this functionality in 
Nim. And even more: I want to declare "half-anonymous" reference instead of 
pointer.

I use "half-anonymous" term to distinguish from "fully-anonymous" pointer which 
is simply `void*`.

Here what I figured out so far, using "fully-anonymous" pointers in Nim:

* * *

warehouse.nim: 
    
    
    # Does Nim allow to declare half-anonymous pointer like in 'C' below?
    # typedef   struct BoxOfStuff *   pBox;
    
    # no need to include file where contents of BoxOfStuff is
    # defined since here we don't care
    
    #struct Warehouse {
    #  pBox shelf[];   #c-compiler ensures type of items is pBox. What about 
Nim?
    #}
    type
        Warehouse = object
            shelf : seq[pointer]  # I want anonymous references with type 
checking here. How?
        
        rWarehouse = ref Warehouse
    

box_of_eggs.nim: 
    
    
    import warehouse
    # this file is not imported by warehouse.nim
    
    type
        BoxOfEggs = object
            # some stuff
        rBoxOfEggs = ref BoxOfEggs
    
    # example push/pop functions allowing type checking:
    #
    proc pushbox* (w:rWarehouse, b:rBoxOfEggs) {.inline.} =
        GC_ref(b)   # NOTE: it will crash without it, because we assign into 
anonymous ptr
        w.shelf.add(cast[pointer](b))
    
    proc popbox* (w:rWarehouse) : rBoxOfEggs {.inline.} =
        result = cast[rBoxOfEggs](w.shelf.pop)
        GC_unref(result)
    

Reply via email to