Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
November 10, 2013 dchar literals? | ||||
---|---|---|---|---|
| ||||
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... |
November 10, 2013 Re: dchar literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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
|
November 10, 2013 Re: dchar literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 |
November 11, 2013 Re: dchar literals? | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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.
|
November 11, 2013 Re: dchar literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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
|
November 12, 2013 Re: dchar literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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
|
November 12, 2013 Re: dchar literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | 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.
> >
> > 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
That's more verbose, but it's something that I think we need anyway, and it might be enough to make it not worth adding the suffixes to character literals.
- Jonathan M Davis
|
November 12, 2013 Re: dchar literals? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | 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
|
Copyright © 1999-2021 by the D Language Foundation