Shouldn't hasSwappableElements work on char arrays?
Jonathan M Davis
jmdavisProg at gmx.com
Thu Feb 24 12:14:05 PST 2011
On Thursday, February 24, 2011 11:42:38 Andrej Mitrovic wrote:
> On 2/24/11, Jesse Phillips <jessekphillips+D at gmail.com> wrote:
> > Andrej Mitrovic Wrote:
> >> Now I see why using char[] fails. It's because [ElementType!(R).init];
> >> returns a dchar[].
> >
> > Yep, Unicode for the win. dchar[] is swappable.
>
> Oh looks like you're right. I can use reverse on dchar[]. Weird, I
> thought I've already tried that.
>
> P.S. Why do I have to use this gibberish syntax?:
>
> dchar[] test = to!(dchar[])("test");
>
> Isn't the compiler smart enough to do this for me automatically? It's
> a string literal..
It's because the type of an expression has nothing to do with what it's assigned
to. So, the type of "test" is string, not dchar[] (on top of the fact that - on
Linux at least - "test" _is_ immutable, so assigning it to a dchar[] without
duping it is bad anyway). So, the result of the expression on the right-hand
side of the assignment does _not_ match the type of the variable being assigned
to (or initialized in this case).
> Still, I don't see why char arrays should fail on hasSwappableElements
> when swap can be used on char arrays?
It's because char arrays are ranges of dchar, _not_ char. So, you _can't_ swap
them. And since each individual char is potentially meaningless on its own
(since in anything other than straight ASCII, you're going to need multiple
chars per character), swapping them makes no sense. hasSwappableElements deals
with ranges, not arrays. And char[] is a range of dchar, not char.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list