Is [] mandatory for array operations?
Robert Jacques
sandford at jhu.edu
Thu May 6 14:07:41 PDT 2010
On Thu, 06 May 2010 16:48:30 -0400, Don <nospam at nospam.com> wrote:
> Robert Jacques wrote:
>> On Thu, 06 May 2010 14:40:17 -0400, bearophile
>> <bearophileHUGS at lycos.com> wrote:
>>> Kagamin:
>>>> Since sin's result is not an array,<
>>>
>>> I am not sure, but I presume here Don was talking about a dispatch of
>>> the sin to all items of an array, so its result is another (new or
>>> mutated in place) array.
>>>
>>> Bye,
>>> bearophile
>>>
>> That presumes a transform of the form: sin(y[]) => map!sin(y).
> However,
>> this creates unnecessary temporaries
>
> Why do you say that? It's inside an array operation, so it's already
> inside a loop.
> a[] = c[] + sin(b[])[];
> ===>
> for(int i=0; i< a.length; ++i)
> {
> a[i] = c[i] + sin(b[i]);
> }
> No temporaries.
> (One important possibility is that the compiler can synthesize array
> operations from pure functions, ie there is no defined function
> double[] sin(double[] x);
> ).
>
> This is simply a question of syntax. Does it need the [] to resolve
> syntactic ambiguity? I don't think it does, but I have some uncertainty
> because of the property case.
bearophile was making an argument for the sin(b[])[] syntax based on
sin(b[]) producing an array of some kind. If true, this would make
sin(b[]) a valid syntactic construct outside of other array ops, therefore
implying temporaries due to some intermediate wrapper struct. Like
map!sin(y).
On a similar note:
z[0..2] = x[0..2] + y[0..2];
is valid so therefore
a[0..2] = c[0..2] + sin(b[5..10])[1..3];
should be valid, and that looks like a very bad idea to me.
More information about the Digitalmars-d
mailing list