[Issue 3395] Ambiguous array operations

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Sep 11 00:09:10 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=3395

--- Comment #6 from Sobirari Muhomori <dfj1esp02 at sneakemail.com> ---
(In reply to Stewart Gordon from comment #4)
> > Brainstorming a few possibilities:
> > 
> >  y[] = max(x[2..12]);     // (1) looks like scalar assignment
> >  y[] = max[2..12](x);     // (2)
> >  y[] = max(x[2..12])[];   // (3)
> 
> That's ambiguous - maybe max is a function that returns an array or other
> type with an opSlice().

This is exactly what happened: issue 11244

(In reply to Stewart Gordon from comment #2)
> (In reply to comment #0)
> > These expressions are ambiguous:
> > ---
> > a[].max(n);
> > a[1..4].max(n);
> > ---
> > Does it mean calling the function on the slice or on each item in the slice?
> 
> It means calling the function on the slice.  Unless I'm mistaken, there
> isn't any D syntax at the moment that means calling the function on each
> element of the array.

The problem is that iteration is not expressed in the syntax, and the compiler
is free to interpret it in any way it feels like.

So the idea is to give iteration distinct syntax:
x[*]=y[*]; //copy slice
x[*]=a; //assign all slice items
f(x[*]); //rewritten as foreach(a;x)f(a);
x[*]=f(y[*]); //foreach(i,ref a;x)a=f(y[i]);
x[*]=f(y[]); //foreach(ref a;x)a=f(y.opSlice());
x[][][][*]=a; //x.opSlice().opSlice().opSlice()[*]=a;

--


More information about the Digitalmars-d-bugs mailing list