Fixed-size arrays and 'null'

Max Samukha spambox at d-coding.com
Thu Nov 12 09:55:28 PST 2009


On Thu, 12 Nov 2009 07:42:57 -0500, bearophile
<bearophileHUGS at lycos.com> wrote:

>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

I'm not sure disallowing 'null' for T[] return values is a good idea.
What's the advantage?

Anyway, it's a different matter. Mingling *fixed-size* arrays with
null does make little sense.



More information about the Digitalmars-d mailing list