www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dchar literals?

reply "Philippe Sigaud" <philippe.sigaud gmail.com> writes:
OK, maybe I'm slow today. Is there an easy way to write a dchar 
literal?

I tend to use either:

"a"d[0]

or:

cast(dchar)'a'

Because 'a'd does not work...
Nov 10 2013
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, November 10, 2013 22:13:04 Philippe Sigaud wrote:
 OK, maybe I'm slow today. Is there an easy way to write a dchar
 literal?
 
 I tend to use either:
 
 "a"d[0]
 
 or:
 
 cast(dchar)'a'
 
 Because 'a'd does not work...
I always do the cast, though honestly, I think that character literals should default to dchar rather than char. I'm not sure that we could ever talk Walter into that though, particularly if he thought that doing so would break code (I'm not sure whether it would or not, but using char for a character is almost always a bad idea, so defaulting to char for character literals just doesn't make sense to me). I'm not aware of there being a shorter way to get character literal to be dchar, though I suppose that if you had to do it a lot, you could create a function with a short name. e.g. dchar toDC(dchar d) { return d; } and end up with toDC('a') instead of cast(dchar)'a'; but I'd probably just use the cast. It certainly sounds like a nice enhancement though to be able to do 'a'd especially if the c and w versions gave errors when the character wouldn't fit in a char or wchar, which isn't the case with a cast. - Jonathan M Davis
Nov 10 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

 It certainly sounds like a nice enhancement though to be able 
 to do

 'a'd

 especially if the c and w versions gave errors when the 
 character wouldn't fit
 in a char or wchar, which isn't the case with a cast.
I have added your idea to the bug report that I opened time ago: https://d.puremagic.com/issues/show_bug.cgi?id=11103 Bye, bearophile
Nov 10 2013
prev sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sun, Nov 10, 2013 at 10:42 PM, Jonathan M Davis <jmdavisProg gmx.com>wrote:
 I always do the cast, though honestly, I think that character literals
 should
 default to dchar rather than char. I'm not sure that we could ever talk
 Walter
 into that though, particularly if he thought that doing so would break code
 (I'm not sure whether it would or not, but using char for a character is
 almost always a bad idea, so defaulting to char for character literals just
 doesn't make sense to me).

 I'm not aware of there being a shorter way to get character literal to be
 dchar, though I suppose that if you had to do it a lot, you could create a
 function with a short name. e.g.
OK, thanks. I'll go with the cast, as this way anyone reading the code will be clear on what is happening there. And I agree with you than character literals should default to dchar. It's a perpetual source of friction for me.
Nov 11 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Philippe Sigaud:

 And I agree with you than character literals should default to 
 dchar. It's
 a perpetual source of friction for me.
99% of my char literals need to be of type char. On the other hand once you have suffixes to specify the char type, most of that problem vanishes, because writing 'x'c or 'x'w, 'x'd is good. Bye, bearophile
Nov 11 2013
parent reply "Kenji Hara" <k.hara.pg gmail.com> writes:
On Monday, 11 November 2013 at 13:20:04 UTC, bearophile wrote:
 Philippe Sigaud:

 And I agree with you than character literals should default to 
 dchar. It's
 a perpetual source of friction for me.
99% of my char literals need to be of type char. On the other hand once you have suffixes to specify the char type, most of that problem vanishes, because writing 'x'c or 'x'w, 'x'd is good. Bye, bearophile
Or, uniform construction for built-in types would be another better way. auto c = char('a'); auto w = wchar('a'); auto d = dchar('a'); auto x = char('à'); // compile-time error Kenji Hara
Nov 12 2013
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Tuesday, November 12, 2013 09:14:54 Kenji Hara wrote:
 On Monday, 11 November 2013 at 13:20:04 UTC, bearophile wrote:
 Philippe Sigaud:
 And I agree with you than character literals should default to
 dchar. It's
 a perpetual source of friction for me.
=20 99% of my char literals need to be of type char. =20 On the other hand once you have suffixes to specify the char type, most of that problem vanishes, because writing 'x'c or 'x'w, 'x'd is good. =20 Bye, bearophile
=20 Or, uniform construction for built-in types would be another better way. =20 auto c =3D char('a'); auto w =3D wchar('a'); auto d =3D dchar('a'); =20 auto x =3D char('=C3=A0'); // compile-time error
That's more verbose, but it's something that I think we need anyway, an= d it=20 might be enough to make it not worth adding the suffixes to character l= iterals. - Jonathan M Davis
Nov 12 2013
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Kenji Hara:

 Or, uniform construction for built-in types would be another 
 better way.

 auto c = char('a');
 auto w = wchar('a');
 auto d = dchar('a');

 auto x = char('à');   // compile-time error
Yes, uniform construction syntax for all types seems a good idea. But the suffixes for chars, as the strings, could be a good idea any way, it's shorter, and it's symmetric with strings. Bye, bearophile
Nov 12 2013