Fixed-size arrays and 'null'

Max Samukha spambox at d-coding.com
Thu Nov 12 02:18:26 PST 2009


Please consider the following: 

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.

3. Fails, but if 1-2 are ok, I can't see why.

4. Compiles but makes little sense.

5. This may be ok, if 1-2 are ok.

I think all of the above should fail at compile time. Just disallow
null initialization/assignment/comparison for fixed-size arrays.

The new behavior has broken some of my code relying on a type's
nullability and I would like to know if the semantics are officially
approved (provided 3 and 4 are fixed)?




More information about the Digitalmars-d mailing list