Foreach and string to string assignment (maybe a bug..)

bearophile bearophileHUGS at
Thu Oct 14 12:20:06 PDT 2010

Steven Schveighoffer:

> How do you request in a foreach loop that it doesn't dup?

There are several ways to do this. You may add a boolean value in the struct/class template, that switches the safe/unsafe behaviour (defaults to safe), or you may let the basic opApply to dup, plus you add another method with a name that warns against its non-copying behaviour that doesn't dup. In my dlibs1 I have used the first solution for the lazy xpermutations/xcombinations.

> Whether the foreach loop should auto-dup for you is another question, I  
> think it's fine to require you to dup each key's name if you plan on using  
> it later, but this should be in the documentation.

D Zen follows another design philosophy, documentation is not enough, people forget things.

> Note also, by "unsafe" we just mean surprising :)  It's still safe in  
> terms of memory safety.

"Safe" has many different meanings. Something that on default acts in a bug-prone way is not "safe".


More information about the Digitalmars-d-learn mailing list