Question about iteger literals

Uranuz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 22 02:36:27 PDT 2014


I have the following programme

import std.stdio;

bool isDigit(char c) nothrow
{
	return c >= '0' && c <= '9';
}

ushort hexValue(char c) nothrow
{
	if( isDigit(c) )
		return c - '0';
	else if (c >= 'a' && c <= 'f')
		return c - 'a' + 10;
	else if (c >= 'A' && c <= 'F')
		return c - 'A' + 10;
	else
		return ushort.max;
}


void main()
{
	writeln(hexValue('A'));	
}

This example is compiling successfully in DMD 2.064 but in DMD 
2.065 a got the following error:

/d544/f547.d(12): Error: cannot implicitly convert expression 
(cast(int)c - 48) of type int to ushort
/d544/f547.d(14): Error: cannot implicitly convert expression 
(cast(int)c - 97 + 10) of type int to ushort
/d544/f547.d(16): Error: cannot implicitly convert expression 
(cast(int)c - 65 + 10) of type int to ushort

So I have a question why these expressions are casted to int? I 
was thinking that result should be of char type. And it could be 
implicitly converted to ushort (because there is enough place to 
store result). Is it a bug in compiler or I should insert 
explicit casts?


More information about the Digitalmars-d-learn mailing list