On Friday, 28 April 2017 at 22:11:30 UTC, H. S. Teoh wrote:
The latest WAT I found in D is this one, see if you can figure
it out:
char ch;
wchar wch;
dchar dch;
pragma(msg, typeof(true ? ch : ch)); // char - OK
pragma(msg, typeof(true ? ch : wch)); // int - WAT?
pragma(msg, typeof(true ? wch : wch)); // wchar - OK
pragma(msg, typeof(true ? wch : dch)); // uint - WAT?
pragma(msg, typeof(true ? dch : dch)); // dchar - OK
How an alternation between two character types ends up being
int is beyond me, but even more inscrutible is why ch : wch
produces int but wch : dch produces uint.
That's the C integer promotion rule, nothing suprising here.
C99 says "if an int can represent all values of the original
type, the value is converted to an int; otherwise, it is
converted to an unsigned int."
While quite often surprising for people coming from other
languages, I think that Walter's persistence in following the
basic C rule is a good thing.
Maybe the documentation should cite more prominently from the C
standard on that point. While it is quite obvious, I noticed that
a lot of people do not know how it works.