Keyword to avoid not null references

Namespace rswhite4 at googlemail.com
Mon Apr 23 07:31:03 PDT 2012


On Monday, 23 April 2012 at 11:04:24 UTC, Benjamin Thaut wrote:
> 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

Yes, that's what i wrote a site before. Otherwise you couldn't
use it in other modules, that's right.

> 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

That is bad. Without the possibility of such implicit constructs
NotNull isn't usefull at all.
I wouldn't write for all the objects which i would check
"method_with_not_null_object(ConvertToNotNull(f_obj));" That
isn't helpfull, that makes more work as a sugesstion in the
method with assert(obj !is null); and that was what i wanted
avoid.
Why this reluctance against a keyword to check a normal Object
which is passed as parameter, e.g. @notNull Foo f?
I didn't understood it. Please explain that to me.
My previous language was C++ and so my first thoughts were that
only pointer types can be null but not references. And then i've
learned that D allows this behavoiur for both: refernces e.g.
objects that passes as parameter and even for pointer types.
That's a point which i will never understand.



More information about the Digitalmars-d-learn mailing list