How to reverse char[]?

Timon Gehr timon.gehr at gmx.ch
Wed Feb 8 07:34:42 PST 2012


On 02/08/2012 03:56 PM, Steven Schveighoffer wrote:
> On Wed, 08 Feb 2012 04:30:04 -0500, Jos van Uden <user at domain.invalid>
> wrote:
>
>> On 8-2-2012 2:36, Timon Gehr wrote:
>>
>>> char[] is handled by Phobos as a range of dchar, ergo it does not have
>>> swappable elements.
>>
>> I'm surprised that array.reverse does work (using 2.057)
>
> array.reverse is *not* the same as reverse(array). The former is a
> compiler-builtin property for all arrays (the compiler believes that
> anything of the form T[] is an array,

That is obviously the case. It is just that functions taking eg. char[] 
often have an in-contract that the array contains a valid utf-8 string.

> even if it's a narrow-width string
> type),  and the latter is a range function.
>

Luckily, array.reverse is going away. Anyway, note that char[].reverse 
reverses unicode code points, not code units.

> D will continue to trip over itself and fall into newbies until it makes
> a decision to make strings not also be arrays.
>
> -Steve

When I was a newbie, I liked the design of D strings. Anyway it is not 
the case that strings are _also_ arrays. D strings are arrays.


More information about the Digitalmars-d-learn mailing list