On Saturday, 3 March 2018 at 20:52:25 UTC, SimonN wrote:

The pitfall here is that all structs must be default-constructible, and then all of the fields have the static init value:

    class A { this(int) { } }
    void main()
    {
        NonNull!A a;
        assert (a.value is null);
    }

...compiles and passes.

Maybe this part can be fixed with:

struct NonNull(T) if (isPointer!T || is(T == class))
{
    static if (isPointer!T)
        alias U = PointerTarget!T;
    else
        alias U = T;

    T value = new U();

    alias value this;

    @disable this(typeof(null)); // no no

    this(Args...)(Args args) {
        this.value = new U(args);
    }

    @disable opAssign(V)(V v) {} // no no no

    void opAssign(V)(NonNull!V other) { // only this allowed.
        this.value = other.value;
    }
 }

Tested a bit this time and added a few extra checks. (thinking out loud: Maybe the constraint should only be is(T == class) actually and we can keep this to reference types...)

The above will also fail right now if T is a class with a non default init. Maybe there's a way to fix that as well though.



Wrapping class references in structs is costly, we lose a lot of automatic conversions across subclasses, const/immutable,

Ok this part would require some testing and playing around, can't really think of anything off the top of my head.

Can you provide an example case here? It might be workable with some (hurts my head to think about right now) combinations of auto ref/inout on initializers or opAssigns or something.

No ideas for now, but I'll certainly follow this development closely, and will be happy to test things!

Glad to hear!



Reply via email to