Is [] mandatory for array operations?

Jason House jason.james.house at gmail.com
Thu May 6 21:13:04 PDT 2010


Robert Jacques Wrote:

> On Thu, 06 May 2010 22:46:42 -0400, Michel Fortin  
> <michel.fortin at michelf.com> wrote:
> 
> > On 2010-05-06 21:48:09 -0400, "Robert Jacques" <sandford at jhu.edu> said:
> >
> >> On Thu, 06 May 2010 20:57:07 -0400, Michel Fortin   
> >> <michel.fortin at michelf.com> wrote:
> >>
> >>> On 2010-05-06 19:02:03 -0400, Jason House  
> >>> <jason.james.house at gmail.com>  said:
> >>>
> >>>> Don Wrote:
> >>>>
> >>>>>  x[] = sin(y[]);
> >>>>  I strongly favor the first syntax since it matches how I'd write it  
> >>>> in  a for loop.
> >>>> i.e. I'd replace [] with [i].
> >>>  This is the best way to see array operations I've read up to now:   
> >>> replace [] with [i], i being the current loop index. It's so simple  
> >>> to  explain.
> >>>
> >>>> If there was a sin variant that took array input, then I'd expect  
> >>>> the  line to be:
> >>>>   x[] = sin(y)[]
> >>>>  which would translate to creating a temporary to hold sin(y) array.
> >>>  Makes sense too.
> >>  this:
> >> for(int i = 0; i < x.length; i++) {
> >>      x[i] = sin(y)[i];
> >> }
> >>  makes sense?
> >
> > Yes, if as stated by Jason there was a sin variant that took array input.
> >
> > That said, I'd expect the compiler to call sin(y) only once, so it'd be  
> > more like that:
> >
> > 	auto sinY = sin(y);
> > 	for(int i = 0; i < x.length; i++) {
> > 		x[i] = sinY[i];
> > 	}
> >
> 
> Ah, thank you for the clarification. I mis-read the first post. Although,  
> for sin(y)[] to work, the variant would have to return an array in  
> addition to taking one.
> 
> hmm...
> given
> real foo(real value) {...}
> and
> real[] foo(real[] value) {...}
> 
> what should happen with the follow line of code:
> 
> x[] = foo(y[]) + z[];

That can be interpreted in either of the following ways:

foreach(i) x[i] = foo(y[i]) + z[i];
OR
auto F = foo(y[]); foreach(i) x[i] = F + z[i];

The 2nd would be a compile error, so it must be the first.


More information about the Digitalmars-d mailing list