Since many people think that non-nullable references can be implemented as
a library and thus don't belong to core language, I've decided to show
that it is in fact impossible to do so.
How do you enforce the following behavior:
class Foo
{
this()
{
// error: variable nonNull not initialized
}
this()
{
nonNull.someMethod(); // error: variable used before initialized
auto s = toString(); // error: can't call any methods before
initialized
nonNull = new Bar();
s = toString(); // okay
}
string toString() { return nonNull.toString(); }
NonNull!(Bar) nonNull;
}
class Bar : Foo
{
this()
{
nonNull.someMethod(); // error: variable used before initialized
super(); // initializes nonNull
nonNull.someMethod(); // fine
}
}
Without support of these use-cases NonNull!(T) is useless.
There can be other examples, but I think these are enough to prove that
non-nullable references can not be implemented in library.