Fixed-size arrays and 'null'

bearophile bearophileHUGS at lycos.com
Thu Nov 12 04:42:57 PST 2009


Max Samukha:

> import std.stdio;
> 
> void foo(int[3] a) {}
> 
> void main()
> {
>     int[3] a = null; // 1
>     a = null; // 2
>     foo(null); // 3
>     if (a is null){} // 4
>     if (a == null){} // 5
> }
> 
> 1 - 2. These compile and issue runtime error. null is implicitly cast
> to int[] and the program tries to assign it at runtime. Even if it is
> not a bug, the error can be detected at compile-time.

In the Delight language this is disallowed:
int[] foo() { return null; }
You must use something like:
int[] foo() { return []; }
That I think is better/more clear.

Bye,
bearophile



More information about the Digitalmars-d mailing list