dchar literals?

Jonathan M Davis jmdavisProg at gmx.com
Sun Nov 10 13:42:23 PST 2013


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


More information about the Digitalmars-d-learn mailing list