Forbid dynamic arrays in boolean evaluation contexts

bearophile bearophileHUGS at lycos.com
Tue Mar 26 16:57:31 PDT 2013


Timon Gehr:

> IOW, what is the point of "null" if you can just use [].

I usually prefer to use [], because null is a literal for 
pointers and class references, while [] is a literal specific for 
arrays (and strings), so its meaning is more clear (in D I'd even 
like a [:] literal that represents an empty associative array).

On the other hand if you compile a program that uses null instead 
of [] you see some differences. In the current dmd compiler 
returning null is more efficient. I have seen code where this 
difference in performance matters:


int[] foo() {
     return [];
}
int[] bar() {
     return null;
}
void main() {}



_D4temp3fooFZAi:
L0:     push    EAX
         mov EAX,offset FLAT:_D11TypeInfo_Ai6__initZ
         push    0
         push    EAX
         call    near ptr __d_arrayliteralTX
         mov EDX,EAX
         add ESP,8
         pop ECX
         xor EAX,EAX
         ret

_D4temp3barFZAi:
         xor EAX,EAX
         xor EDX,EDX
         ret

Bye,
bearophile


More information about the Digitalmars-d mailing list