UFCS idea

Adam D. Ruppe destructionator at gmail.com
Wed Jun 8 17:29:39 PDT 2011


>   Foo@ foo; // An error, because foo would be null.

I believe we're getting this in the library soon. I saw disabling
the default constructor on a todo list not long ago.

By disabling the default constructor, a library construct will be
able to reliably detect null at construction time:

struct NotNull(T) {
   @disable this() {}
   this(T t) { assert(t !is null); payload = t; }
   T payload;
   alias payload this;
}

NotNull!(int*) a; // error - default constructor is disabled

int* aPtr = null;
auto a = NotNull(aPtr); // assert triggers at runtime - a is never null



The type system may or may not catch nulls - depends on the specific
implementation of the struct - but worst case, it will be noticed
immediately at runtime when you try to store it.


More information about the Digitalmars-d mailing list