| Thread overview | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 20, 2008 automatic conversion to invariant (string?) | ||||
|---|---|---|---|---|
| ||||
Hello, in D2, when you use types as string, wstring (which are invariant(char)[]), there is no autocast from char[] to invariant(char)[] (or const(char)[]), wouldn't this 'downcast' be logical?' -- Yossarian Tato zpráva byla vytvořena převratným poštovním klientem Opery: http://www.opera.com/mail/ | ||||
March 20, 2008 Re: automatic conversion to invariant (string?) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yossarian | On 20/03/2008, Yossarian <xtauer01@stud.fit.vutbr.cz> 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"
| |||
March 20, 2008 Re: automatic conversion to invariant (string?) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yossarian | On 20/03/2008, Yossarian <xtauer01@stud.fit.vutbr.cz> wrote:
> there is no autocast from char[] to invariant(char)[] (or const(char)[]),
char[] will implicitly cast to const(char)[], no problem.
| |||
March 20, 2008 Re: automatic conversion to invariant (string?) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yossarian | On Thu, 20 Mar 2008 13:23:16 +0300, Yossarian <xtauer01@stud.fit.vutbr.cz> wrote:
> Hello,
> in D2, when you use types as string, wstring (which are invariant(char)[]),
> there is no autocast from char[] to invariant(char)[] (or const(char)[]), wouldn't
> this 'downcast' be logical?'
>
> -- Yossarian
> Tato zpráva byla vytvořena převratným poštovním klientem Opery: http://www.opera.com/mail/
There is a difference between const and invariant. Const means that "you shouldn't try to change it" whereas invariant means "this won't change ever". At least, that's how I understand this.
| |||
March 20, 2008 Re: automatic conversion to invariant (string?) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Dne Thu, 20 Mar 2008 12:06:35 +0100 Janice Caron <caron800@googlemail.com> napsal/-a: > On 20/03/2008, Yossarian <xtauer01@stud.fit.vutbr.cz> wrote: >> there is no autocast from char[] to invariant(char)[] (or const(char)[]), > > char[] will implicitly cast to const(char)[], no problem. but string is invariant(char)[]. and that's problem for me, because some of the standard libraries aren't ready for this. -- Tato zpráva byla vytvořena převratným poštovním klientem Opery: http://www.opera.com/mail/ | |||
March 20, 2008 Re: automatic conversion to invariant (string?) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Dne Thu, 20 Mar 2008 12:05:44 +0100 Janice Caron <caron800@googlemail.com> napsal/-a: > On 20/03/2008, Yossarian <xtauer01@stud.fit.vutbr.cz> 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), // int[] c = [3, 5]; compiles. 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. -- Tato zpráva byla vytvořena převratným poštovním klientem Opery: http://www.opera.com/mail/ | |||
March 20, 2008 Re: automatic conversion to invariant (string?) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yossarian | On 20/03/2008, Yossarian <xtauer01@stud.fit.vutbr.cz> wrote:
> but string is invariant(char)[]. and that's problem for me, because some
> of the standard libraries aren't ready for this.
Specifically, which ones?
| |||
March 20, 2008 Re: automatic conversion to invariant (string?) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yossarian | "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 | |||
March 20, 2008 Re: automatic conversion to invariant (string?) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yossarian | On 20/03/2008, Yossarian <xtauer01@stud.fit.vutbr.cz> wrote: > > char[] s = cast(char[]) "hello"; > > i think that > char[] s = "hello" ; // this should compile without explicit cast. imho. In fact, I just wrote that for illustration purposes. I was trying to imply that s was a char[], pre-initialised with "hello". In real code, you should /never/ do char[] s = cast(char[]) "hello"; so I was guilty of using a really bad example. The reason you should never use it is because casting away invariance is undefined, and in this case, the literal might be in a ROM segment. The correct way to do it should be, as others have pointed out: char[]s = "hello".dup; > // int[] c = [3, 5]; compiles. In the future, it probably won't. Don't rely on it. > string t = s; // wouldn't it be logical to use not copy-on-write, but > initialize new string with old char[]? Since that line won't compile anyway, the question is moot. > s[0] = 'j'; // change only s, not t. No can do. A string is nothing but a struct consisting of { ptr, length }. Element assignment should not cause a heap allocation. | |||
March 20, 2008 Re: automatic conversion to invariant (string?) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Dne Thu, 20 Mar 2008 15:56:01 +0100 Janice Caron <caron800@googlemail.com> napsal/-a: > On 20/03/2008, Yossarian <xtauer01@stud.fit.vutbr.cz> wrote: >> but string is invariant(char)[]. and that's problem for me, because some >> of the standard libraries aren't ready for this. > > Specifically, which ones? I can't, for example do this: throw new Error("This is an text of error"); I must do throw new Error(cast(string)"This is an text of error"); -- Tato zpráva byla vytvořena převratným poštovním klientem Opery: http://www.opera.com/mail/ | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply