Keyword to avoid not null references
Benjamin Thaut
code at benjamin-thaut.de
Mon Apr 23 04:04:22 PDT 2012
Am 23.04.2012 09:14, schrieb Namespace:
> I made several tests with NotNull yesterday and actually they all passed.
> In special cases i didn't get a compiler error but then a runtime error
> is better then nothing. :)
>
> But there is still my problem with this:
>
> void foo(NotNull!(Foo) n) {
>
> }
>
> void bar(Foo n) {
>
> }
>
> in my optinion it must exist a way that both
> NotNull!(Foo) nf = new Foo();
>
> foo(nf);
> bar(nf);
>
> and furhtermore
> Foo f = new Foo();
>
> foo(f);
> bar(f);
>
> compiles.
> We need some hack, implicit cast or compiler cast that cast or passes
> Foo to NotNull!(Foo).
>
> Any suggestions?
If you replace
alias _notNullData this;
with
@property T _notNullDataHelper()
{
assert(_notNullData !is null);
return _notNullData;
}
alias _notNullDataHelper this;
It will not be possible to assign to _notNullData
It will still be possbile to use from other modules
NotNull!T will implicitly convert to T (your first case)
However T will not implicitly convert to NotNull!T (as far as I know
such a implict conversion is not possible in D, i suggested a @implicit
modifier for a constructor to allow such implicit type conversions, but
it was rejected)
Kind Regards
Benjamin Thaut
More information about the Digitalmars-d-learn
mailing list