Default struct constructor
bitwise
bitwise.pvt at gmail.com
Tue Feb 12 17:46:16 UTC 2019
On Friday, 8 February 2019 at 19:24:00 UTC, Victor Porton wrote:
> On Friday, 8 February 2019 at 19:20:59 UTC, psycha0s wrote:
>> What kind of problems a default struct constructor could
>> solve? If you want to use RAII, you need to pass the resource
>> as an argument. If you want to set some initial state, you can
>> do it by assigning values to the struct members directly.
>
> In my particular case I need RAII initializing a C library
> object. Some of the C constructor functions don't have
> arguments. So the only way to do it is my silly hack with Dummy
> object. I would probably vote for a change in D spec.
I've boiled my requirement down to structs having a "lifetime
starting" method of some kind. I don't mind if T.init is blitted
over my object before it's called, as long as I can initialize
any struct fields that couldn't be initialized inline due to
their value not being CTFE'able.
I suppose that for this to be worth doing though, it would need
to work extern(D) as well. So creating the object in D would blit
T.init to the struct first, while creating the same object in C
would not. So I'm wondering if this difference would be a deal
breaker (I don't believe it should be), or if what I'm suggesting
creates other technical problems (I can't think of any).
More information about the Digitalmars-d
mailing list