Shouldn't hasSwappableElements work on char arrays?

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Feb 24 13:21:08 PST 2011


On 2/24/11, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
> But what you are asking is for the compiler to implicitly dup it.

Only when the lhs is a mutable type. If it's immutable (string), then
you don't have to dup it. Hence:

string a = "abc";
string b = "abc";
assert(&a[0] == &b[0]);

There's no point in duping the literal in this case, it would just waste memory.

>I have
> thought this might be good to have in the past as well, but it's also not
> too bad to have to type "test"d.dup.  So while having the compiler save
> you a bit of typing would be good, it's not the end of the world to
> require it.
>

Of course it's not that hard. But when things can be safely automated,
I don't see why they shouldn't be. Unless I'm missing some important
factor of duping string literals that was not mentioned already.

Btw, "test"d.dup is actually pretty nice. I would have used it before,
but I didn't know I could use a postfix form /and/ dup it like that.


More information about the Digitalmars-d mailing list