foreach ref very broken: fails to call front(val)

Jonathan M Davis jmdavisProg at gmx.com
Tue Jul 10 18:06:48 PDT 2012


On Wednesday, July 11, 2012 00:15:34 Timon Gehr wrote:
> On 07/10/2012 11:51 PM, Jonathan M Davis wrote:
> > On Tuesday, July 10, 2012 23:25:41 monarch_dodra wrote:
> >> ...
> >> And more generally, how would code like this _ever_ work?
> >> 
> >> Array!int arr
> >> foreach(ref a; arr)
> >> a += 5;
> >> 
> >> Since there is no mechanism to pass the "op" to "front"?
> > 
> > I believe that according to TDPL, if you use ref in a foreach with a
> > range,
> > and range.front = value isn't legal, then you should get a compilation
> > error. Whether a += value works depends on whether range.front += value
> > works, and at present, that's pretty much only going to work with ranges
> > whose front returns by ref, since property syntax isn't currently
> > sophisticated enough to combine the getter and setter properties to make
> > stuff like front++ or front += 5 possible.
> > 
> > - Jonathan M Davis
> 
> That is a bug. @property has very limited value if pairs of @property
> functions are accessed in a way syntactically distinguishable from
> field access.

I don't know if it's technically a bug or not. That would depend on the spec, 
and I suspect that it's mum on the matter, which would make the compiler 
technically correct no matter what it does with regards to this, since TDPL 
doesn't say anything on it either that I recall. But if it's not a bug, it's 
definitely an enhancement request that should be seriously considered. I 
believe that there are a couple of related enhancement requests, but I can't 
find them at the moment.

- Jonathan M Davis


More information about the Digitalmars-d mailing list