automatic conversion to invariant (string?)
Steven Schveighoffer
schveiguy at yahoo.com
Thu Mar 20 08:09:09 PDT 2008
"Yossarian" wrote
> Dne Thu, 20 Mar 2008 12:05:44 +0100 Janice Caron napsal/-a:
>
>> On 20/03/2008, Yossarian wrote:
>>> wouldn't
>>> this 'downcast' be logical?'
>>
>> No. Example:
>>
>> char[] s = cast(char[]) "hello";
>> string t = s;
>> s[0] = 'j';
>>
>> There's a good reason why that won't compile!
>>
>> Both mutable and invariant will downcast to const, so from const to
>> either mutable or invariant is an upcast. That makes going from
>> mutable to invariant a "sideways cast"
>
>
> i think that
> char[] s = "hello" ; // this should compile without explicit cast. imho.
> the cast should be like ("hello".dup),
What's wrong with using "hello".dup explicitly?
> // int[] c = [3, 5]; compiles.
This I think will be changed once Walter is polishing up D2. Any array
literal should be treated consistently.
> string t = s; // wouldn't it be logical to use not copy-on-write, but
> initialize new string with old char[]?
> s[0] = 'j'; // change only s, not t.
This goes against the whole premise of how arrays work in D. You are given
a great feature because heap copies don't happen automatically, and so if
you don't need the copies, then you aren't penalized for it. If you need
them, it's easy to code because dup is builtin to every array.
-Steve
More information about the Digitalmars-d
mailing list