foreach by value iteration

Peter Alexander at
Mon Feb 27 16:22:24 PST 2012

On Sunday, 26 February 2012 at 23:20:24 UTC, James Miller wrote:
> I'm with Jonathon, using `ref` is the best way, it is 
> ridiculous to
> think that the compiler should be able to figure out what's 
> going on
> in every case. After getting hit by it a couple times, then 
> you'll
> remember to use ref if you want to alter a copy.

There's no need for the compiler to figure anything out. I was 
curious if there were any syntactic ways of solving this problem. 
Changing the existing syntax is not really an option, but 
brainstorming ideas can't hurt.

I agree that I can "just remember", but that's not a good way to 
go about creating robust software. People make mistakes. D 
already has lots of errors to help prevent against these kinds of 
common mistakes and making the obvious thing correct and safe is 
one of D's design goals.

> By the way, its the same in other languages, you need to use
> "reference" syntax to alter values in a loop.

C# isn't the same. If you iterate over structs you cannot modify 
the iteration variable. You have to explicitly copy.

More information about the Digitalmars-d mailing list