[Issue 3395] Ambiguous array operations

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Nov 30 00:31:18 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=3395


Don <clugdbug at yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug at yahoo.com.au


--- Comment #3 from Don <clugdbug at yahoo.com.au> 2011-11-30 00:30:12 PST ---
(In reply to 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.

That's correct.

> > Possible solution is to change the meaning of empty square brackets from full
> > slice to only a hint for array operation so that a[].max(n) is an array
> > operation and a[1..4].max(n) is max(a[1..4],n).

> This would get confusing.  You might want to apply a function to the whole
> slice [1..4] or to each element of the slice.  This applies whether the
> array-property sugar is being used or not.
> 
> Perhaps the best solution is to define [] applied to the function identifier
> itself to do an elementwise application.
> 
> So max(a, n) or a.max(n) would just call max(a, n) once.
> And max[](a, n) or a.max[](n) would evaluate to an array of max(a[i], n).
> And the same if a is replaced with a[], a[1..4] or some such in each case.

That looks to me as if max is an array of some struct S which defines an
opCall.

> Of course, ambiguities can still occur in functions with multiple array
> parameters.  Presumably the language would forbid it in these ambiguous cases,
> as it does already with ambiguous overload matching.

Consider the case where we want y to be 
[ max(x[2][0..$], max(x[3][0..$], ... ]

double [][20] x;
double [10] y;

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)
 y[] = max([] x[2..12]);  // (4)
 y[] = max([] x[2..12])[]; // (5) messy!


(2) does looks like an opCall on array called 'max'. 
(3) looks the most intuitive to me. Not perfect though (I don't think we'd want
y[] = max(x[2..12]); to compile and be a scalar).
(4) is an interesting possibility. Doesn't look great, but it seems to be a
syntax hole. Ambiguous in the one-argument property case: x.max([]) could be:
     max([] x)   or  max(x, []) where the [] is an empty array literal.
I think that's solvable though. Interestingly it's the case where (2) is
cleanest: x.max[];

Can we put the [] _before_ the call? y[] = [] max(x);
y[] = x.[]max;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list