dereferencing null

Timon Gehr timon.gehr at gmx.ch
Tue Mar 6 09:19:37 PST 2012


On 03/06/2012 04:46 PM, 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;
> }
>

Right. This example already demonstrates some simplistic data flow analysis.


> 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.

Actually it requires some thinking because making initialization of 
non-null fields safe is not entirely trivial.

For example:
http://pm.inf.ethz.ch/publications/getpdf.php/bibname/Own/id/SummersMuellerTR11.pdf

CTFE and static constructors solve that issue for static data.


More information about the Digitalmars-d mailing list