On 08/03/2010 12:32 AM, bearophile wrote:
Pelle:
I think a good thing would be NonNull!T, but I haven't managed to create
one. If this structure exists and becomes good practice to use, maybe we
can get the good syntax in D3. In 20 years or so :P
Maybe we are talking about two different things, I was talking about nonnull
class references/pointers, you seem to talk about nullable values :-) Both can
be useful in D, but they are different things.
Nullable values are simpler to design, they are just wrapper structs that
contain a value plus a boolean, plus if you want some syntax sugar to manage
them with a shorter syntax.
Bye,
bearophile
I am talking about non-nullable references indeed. I don't think I
mentioned nullable types, really.
I also created this, as the simplest NotNull-type concievable:
struct NotNull(T) if(is(typeof(T.init !is null))) {
private T _instance;
this(T t) {
enforce(t !is null, "Cannot create NotNull from null");
_instance = t;
}
T get() {
assert (_instance !is null,
text("Supposed NotNull!(", T.stringof, ") is null"));
return _instance;
}
alias get this;
}
This has the obvious bug in that you can declare a nonnull without an
initializer and get a null from it. If we ever get @disable this(){} for
structs, this struct can become better.
I'll probably try it out in some code.