www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - automatic conversion to invariant (string?)

reply Yossarian <xtauer01 stud.fit.vutbr.cz> writes:
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/
Mar 20 2008
next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
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"
Mar 20 2008
next sibling parent reply Yossarian <xtauer01 stud.fit.vutbr.cz> writes:
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/
Mar 20 2008
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"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
Mar 20 2008
prev sibling parent Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
Janice Caron wrote:
 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"

Just for the sake of pendanticness, you're switching/mixing up the terms "downcast" and "upcast", it should be the other way around: "Both mutable and invariant will upcast to const, so from const to either mutable or invariant is an downcast." becausing upcasting is generelly the safe cast (less specific), while downcasting is the unsafe cast (more specific). -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Mar 25 2008
prev sibling next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
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.
Mar 20 2008
parent reply Yossarian <xtauer01 stud.fit.vutbr.cz> writes:
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/
Mar 20 2008
parent reply Yossarian <xtauer01 stud.fit.vutbr.cz> writes:
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/
Mar 20 2008
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Yossarian wrote:
 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'm pretty sure you can, actually. You can't do it with a char[] parameter, but literals have invariant characters already and should work fine.
 I must do
 throw new Error(cast(string)"This is an text of error");

String literals implicitly convert to string, so the cast is completely unnecessary.
Mar 20 2008
prev sibling next sibling parent "Koroskin Denis" <2korden+dmd gmail.com> writes:
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.
Mar 20 2008
prev sibling next sibling parent "Janice Caron" <caron800 googlemail.com> writes:
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?
Mar 20 2008
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
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.
Mar 20 2008