Using a struct as a wrapper for an extern(C) opaque type, no default constructor, what do?

Jeremy DeHaan via Digitalmars-d-learn digitalmars-d-learn at
Wed Dec 16 20:05:30 PST 2015

On Thursday, 17 December 2015 at 03:43:58 UTC, Jakob Ovrum wrote:
> On Thursday, 17 December 2015 at 03:31:37 UTC, Jeremy DeHaan 
> wrote:
>> Hi all. I'm interfacing to some C code which include an opaque 
>> type and some C functions that create and work with a pointer 
>> to that type. I want to wrap up everything in a struct, and 
>> the only thing that seems to bug me is initialization.
>> Since it is C code, I obviously can't read the function that 
>> creates the opaque type. Not only that, I can't define a 
>> default constructor. What are my options here?
>> This is for an API that is intended to be used by people other 
>> than myself, so I'd like to use something that doesn't look 
>> ugly or isn't a hack. I really don't like the idea of using a 
>> factory method or overriding opCall. Am I basically out of 
>> luck and need to resort to one of these methods?
> Using a factory function alongside @disable this(); is the 
> canonical way to do this. Although, if your struct is a 
> reference type, you can simply allow default construction and 
> have it mean a null reference.
> Using static opCall here is just a factory function with 
> special syntax, but I think it does more harm than good.

Thanks. I guess what bugs me is that I always try to hide the 
fact that the API is a wrapper around C stuff, ie, I want to make 
people feel as though they're using idiomatic D. Doing something 
like this makes it feel like less idiomatic D and more like a 
wrapper. I think I have a solution that I like in my own case 
though. Right now I'm considering something like this:

More information about the Digitalmars-d-learn mailing list