On Tuesday, 31 January 2017 at 23:52:31 UTC, Ali Çehreli wrote:
On 01/31/2017 03:15 PM, bitwise wrote:
[...]

Thanks for the response, but this doesn't really solve the problem.

> If the object is defined at module scope as shared static > immutable It is indeed possible to initialize immutable objects by pure functions as done inside shared static this() below:

I didn't mean that I wanted my object shared-static-immutable, but only that a solution would have to account for that possibility.

Yes, the situation is different from C++ but it's always possible to call a function (which constructor is one) to make the object.

I'm saying that a caller should not have to explicitly initialize an object before use, but that a programmer should not have to add boilerplate to deal with zombie objects all over the place either.

A container for example:

struct Container(T) {
void pushBack(T); // ok: mutable method, can lazily initialize payload.

    // not ok: container may be immutable
    Range!(const T) opSlice() const;
    Iterator!(const T) find(T) const;
    bool empty() const;
    size_t count() const;
}

Container!int c; // = Container!int() -> can't do this.
if(c.empty) // can't initialize here either..
     c.pushBack(1);

Such innocent looking code will fail without boilerplate inserted everywhere.
-I can't lazily initialize the container in "empty()".
-I can't pre-emptively initialize it in a default constructor.

This problem causes the propagation of null checks all over the place. Objects returned from the container will have to have a "zombie" state as well, and check validity at each use.

I wouldn't classify this as "a difference", but as a hole.

Although I don't remember where, I recently saw a discussion about how "mutable" may possibly be implemented. IIRC, there was no solution stated in that discussion.

The only solution that comes to mind would be to somehow relax the constraints of const, and make it possible to prevent a struct from being declared immutable, so that lazy initialization could be done.

Recent discussions seem to indicate structs having default ctors is not an option.

Reply via email to