On Mon, Nov 22, 2010 at 1:08 AM, Andrei Alexandrescu <
seewebsiteforem...@erdani.org> wrote:

> On 11/21/10 11:59 PM, Rainer Deyke wrote:
>
>> On 11/21/2010 21:56, Andrei Alexandrescu wrote:
>>
>>> On 11/21/10 22:09 CST, Rainer Deyke wrote:
>>>
>>>>
>>>>     - When writing code that uses T[], it is often natural to mix
>>>> range-based access and index-based access, with the assumption that both
>>>> provide direct access to the same underlying data.  However, with char[]
>>>> this assumption is incorrect, as the underlying data is transformed when
>>>> viewing the array as a range.  This means that generic code that uses
>>>> T[] must take special consideration of char[] or it may unexpectedly
>>>> produce incorrect results when T = char.
>>>>
>>>
>>> What you're saying is that you write generic code that requires T[], and
>>> then the code itself uses front, popFront, and other range-specific
>>> functions in conjunction with it.
>>>
>>
>> No, I'm saying that I write generic code that declares T[] and then
>> passes it off to a function that operates on ranges, or to a foreach loop.
>>
>
> A function that operates on ranges would have an appropriate constraint so
> it would work properly or not at all. foreach works fine with all arrays.


One gotcha that seems to occur here is this code:
foreach(index, character; someString) assert(someString[index] ==
character);

I don't really have much that's meaningful to add to this discussion except
to say that it shouldn't be easy to write code like the above. I spent a few
hours today figuring out why that wouldn't work.

Reply via email to