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.

              • ... Timon Gehr via Digitalmars-d-announce
  • Re: "Competitiv... XavierAP via Digitalmars-d-announce
    • Re: "Compe... Atila Neves via Digitalmars-d-announce
      • Re: "C... Nick Sabalausky (Abscissa) via Digitalmars-d-announce
        • Re: &qu... H. S. Teoh via Digitalmars-d-announce
        • Re: &qu... Ali Çehreli via Digitalmars-d-announce
          • Re:... Nick Sabalausky (Abscissa) via Digitalmars-d-announce
        • Re: &qu... Ola Fosheim Grostad via Digitalmars-d-announce
        • Re: &qu... Nick Sabalausky (Abscissa) via Digitalmars-d-announce
          • Re:... Ola Fosheim Grøstad via Digitalmars-d-announce
        • Re: &qu... Patrick Schluter via Digitalmars-d-announce
          • Re:... Ola Fosheim Grøstad via Digitalmars-d-announce
            • ... Patrick Schluter via Digitalmars-d-announce
              • ... Ola Fosheim Grøstad via Digitalmars-d-announce
              • ... Adam D. Ruppe via Digitalmars-d-announce
          • Re:... H. S. Teoh via Digitalmars-d-announce

Reply via email to