DMD 0.173 release

Bill Baxter wbaxter at gmail.com
Sun Nov 5 13:22:58 PST 2006


Bruno Medeiros wrote:
> Ok, so this one is about a feature of some releases ago, but you're 
> releasing new features faster than what we can keep up and look at :P
> 
> Regarding array literals, it concerns me that each new literal 
> "evaluation" will create a new instance. For example a literal inside a 
> function will allocate a new instance every time the function is called. 
> Likewise a literal inside a loop will allocate a new instance for every 
> cycle of the loop!
> This strikes me as both inefficient (as there is no simple way to use 
> the opposite behavior: static/global instances) and inconsistent: other 
> kinds of literals, like string literals, are not instanced per 
> "evaluation" but instead are single/global/static instances.
> Is there any particular reason for this behavior? Because if not I'm 
> inclined to think it would be better that array literals work as string 
> literals (global/static instances). If the new'ed-instance behavior is 
> needed it could be easily emulated with dup'ing:
>   ar = [1, 4, 9].dup;
> 

Agreed.  It also makes life interesting in that things that depend of an 
array having compile-time constant value fail:

   const bool[3] a = [true,false,true];
   static if(a[0]) {}

(and fail with the misleading error message "expression (a)[0] does not 
evaluate to a boolean").

This is ok though:
   static if([true,false,true][0]) {}

Another thing that should work is:

    static const int[3] a = [4,5,6];
    static const int[a[0]] b = [1,2,3,4];

test.d(17): a is used as a type
test.d(17): can't have associative array key of void[0]

Ok, that's maybe a different issue... but I bet if it weren't trying to 
interpret the above as an associative array it would be complaining that 
the value a[0] isn't constant.  :-)

Ah, here's a better example:

   static const int[2] sizes = [2,4];
   static const int[2] use_sizes = [sizes[0], sizes[1]];

 >> staticdata.d(19): non-constant expression (sizes)[0]
 >> staticdata.d(19): non-constant expression (sizes)[1]

--bb



More information about the Digitalmars-d-announce mailing list