dereferencing null
Jason House
jason.james.house at gmail.com
Mon Mar 5 16:01:19 PST 2012
On Monday, 5 March 2012 at 04:39:59 UTC, Adam D. Ruppe wrote:
> Huh, I thought there was one in phobos by now.
>
> You could spin your own with something like this:
>
> struct NotNull(T) {
> T t;
> alias t this;
> @disable this();
> @disable this(typeof(null));
> this(T value) {
> assert(value !is null);
> t = value;
> }
>
> @disable typeof(this) opAssign(typeof(null));
> typeof(this) opAssign(T rhs) {
> assert(rhs !is null);
> t = rhs;
> return this;
> }
> }
>
The opAssign kills all type safety. I think only NotNull!T should
be accepted... So "foo = bar" won't compile if bar is nullable.
To fix, "foo = NotNull(bar)",
More information about the Digitalmars-d
mailing list