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

Steven Schveighoffer schveiguy at yahoo.com
Thu Oct 14 11:25:58 PDT 2010


On Thu, 14 Oct 2010 13:49:13 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Steven Schveighoffer:
>
>> This is a common problem in defining an opApply loop which streams data  
>> --
>> do you make things safe or efficient?  The only reasonable solution IMO  
>> is
>> to make them efficient, because safety can be had by duping the data.
>
> The only reasonable solution is to make it safe, and then allow the  
> unsafe and more efficient behaviour only on explicit request. This  
> follows D philosophy and in practice it's the only design that allows  
> you to write programs that actually work, instead of being just a nest  
> for bugs.

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

That's why I think the only one which allows both is the by default unsafe  
version.

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


On Thu, 14 Oct 2010 13:55:38 -0400, Andrej Mitrovic  
<andrej.mitrovich at gmail.com> wrote:

> So do I file this as a bug? The current behavior is way too subtle
> imo, at least the compiler could issue a warning if this behavior is
> intended.

Well, at the very least, this violates immutability.  You can file a bug  
based on those grounds alone.

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.

-Steve


More information about the Digitalmars-d-learn mailing list