February 08, 2019
> 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 see. Yeah, I remember I encountered with the need to have a default struct constructor myself earlier, but I couldn't remember the reason. Some of the current D "features" are very inconvenient. Especially the inability to pass an rvalue by a const reference and the forbidden default constructors for structs.
February 10, 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.

You might like this article https://w0rp.com/blog/post/an-raii-constructor-by-another-name-is-just-as-sweet/
February 11, 2019
On Friday, 8 February 2019 at 08:25:18 UTC, Jonathan M Davis wrote:
> It's true that there are use cases where the lack of a default constructor for structs is annoying, but in general, all you have to do is use a factory method instead and then make sure that the type still works if its init value is used (e.g. make sure that the destructor is aware that the factory method can be bypassed). And if you absolutely must have default construction, you can always use a class instead.
>
> - Jonathan M Davis

Just found if you need a type that can be parameterized by an alias to a function that accesses some local frame data, and you want the factory to be static on the type, you can't do it.

Seems like a bug maybe? as I'm not sure what the difference is between the static construct function below or a global one that is parameterized on "alias fun" ?

struct S(alias fun) {
    static auto construct() {
        auto a = S!fun();
        return a;
    }
    void call() { fun(); }
}

void main() {
    int count;
    auto s = S!(() => count++).construct;
}

onlineapp.d(5): Error: cannot access frame pointer of onlineapp.main.S!(delegate () => count++).S

February 12, 2019
On 08.02.19 20:53, psycha0s wrote:
> Some of the current D "features" are very inconvenient.
> [...] inability to pass an rvalue by a const reference

Missing by-reference passing of rvalues is a nuisance, but it has nothing to do with const.
February 12, 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).


February 16, 2019
> pass an rvalue by a const reference and the forbidden default constructors for structs.

Yes please
Next ›   Last »
1 2