Keyword to avoid not null references
Namespace
rswhite4 at googlemail.com
Mon Apr 23 02:02:50 PDT 2012
On Monday, 23 April 2012 at 08:47:22 UTC, Simen Kjaeraas wrote:
> On Mon, 23 Apr 2012 10:38:27 +0200, Dmitry Olshansky
> <dmitry.olsh at gmail.com> wrote:
>
>> On 23.04.2012 12:06, Simen Kjaeraas wrote:
>>> On Mon, 23 Apr 2012 09:14:12 +0200, Namespace
>>> <rswhite4 at googlemail.com>
>>> wrote:
>>>
>>>> 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?
>>>
>>> No. The whole point of NotNull is that it should enforce not
>>> being null.
>>> Allowing implicit casting from PossiblyNull to NotNull would
>>> break this.
>>
>> Just include obligatory run-time check when crossing
>> null-NotNull boundaries.
>>
>
> Which carries with it hidden runtime costs that are
> unacceptable to some.
> The point of NotNull is twofold - safety (you know it's not
> null) and speed
> (you don't need to check if it's null). The latter goes out the
> window if
> implicit casting were allowed.
Allow both: a type for explicit not null which cannot be changed
to null and a keyword or some other construct which can check any
possible object at compile time if it's null.
More information about the Digitalmars-d-learn
mailing list