dereferencing null

Christopher Bergqvist spambox0 at digitalpoetry.se
Tue Mar 6 11:29:32 PST 2012


On Tuesday, 6 March 2012 at 15:46:54 UTC, foobar wrote:
> On Tuesday, 6 March 2012 at 10:19:19 UTC, Timon Gehr wrote:
>
>> This is quite close, but real support for non-nullable types 
>> means that they are the default and checked statically, 
>> ideally using data flow analysis.
>
> I agree that non-nullable types should be made the default and 
> statically checked but data flow analysis here is redundant.
> consider:
> T foo = ..; // T is not-nullable
> T? bar = ..; // T? is nullable
> bar = foo; // legal implicit coercion T -> T?
> foo = bar; // compile-time type mismatch error
> //correct way:
> if (bar) { // make sure bar isn't null
>   // compiler knows that cast(T)bar is safe
>   foo = bar;
> }
>
> of course we can employ additional syntax sugar such as:
> foo = bar || <default_value>;
>
> furthermore:
> foo.method(); // legal
> bar.method(); // compile-time error
>
> it's all easily implementable in the type system.

I agree with the above and would also suggest something along the 
lines of:
assert (bar) { // make sure it isn't null in debug builds
    bar.method(); // legal
}

The branchy null-check would then disappear in build 
configurations with asserts disabled.


More information about the Digitalmars-d mailing list