dereferencing null

foobar foo at bar.com
Tue Mar 6 07:46:53 PST 2012


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.


More information about the Digitalmars-d mailing list