DMD 0.173 release
Bill Baxter
wbaxter at gmail.com
Sun Nov 5 13:41:14 PST 2006
Ivan Senji wrote:
> 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
>
>
> Aint that the truth. :)
>
>>
>> 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;
>>
>
> I'm afraid that things have to be that way. It would make sense for
> array literals to be allocated only once if they were made of only
> constant values, but in cases like:
>
> for(int i=0; i<100; i++)
> {
> f( [i, i*i] );
> }
>
> I would expect a new instance to be created at each step in the loop.
>
It doesn't even need to be dynamic values. Given
for(int i=0; i<100; i++)
{
f( [1, 42] );
}
You might have f modifying the array since D doesn't have good support
for const:
void f(int[2] v) {
v[0]++;
}
And then f([1,42]) after the first call becomes equivalent to f([2,42])
which would be bad.
--bb
More information about the Digitalmars-d-announce
mailing list