Christopher Wright wrote:
BCS wrote:
Hello Walter,
Andrei suggests making a library type work for this rather than a
language attribute, but it's still an extra thing that will have to be
specified everywhere where used.
I've considered trying to make a template that compile time enforces
Non-null usage but can, with a version flag, be switched to a simple
alias/typedef of the internal type. Near zero overhead and possibly
just as strong a guard.
It's impossible.
You can create a struct that will throw an exception if you use it
uninitialized, or if you try assigning null to it. But the struct cannot
require that it is initialized.
You can add a contract that requires the struct be initialized, and put
the contract and declaration in a template.
It is possible (I have one hanging out in my code somewhere). It works
like this:
struct NonNull(T) if is(T == class)
{
T value = cast(T) &NullObject!(T);
...
alias value this; // does not compile yet
}
Constructing a null object is not hard - you can do it with static
this() code, I forgot exactly how I did it.
But this kind of code is not terribly useful, it initializes "null"
objects to correct objects. Instead of getting a hard error, you end up
having odd behavior because you're using that object.
One step forward would be to assign all functions in T's vtable to
throw-everything (make T a white hole object). Still fields will be
changeable.
Andrei