Is this a bug in return type inference?

Meta via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 26 12:32:54 PDT 2015


import std.random;

auto test(int n)
{
     if (n >= 0 && n < 33)
     {
         return int(0);
     }
     else if (n >= 33 && n < 66)
     {
         return float(0);
     }
     else
     {
         return real(0);
     }
}

void main()
{
     auto n = uniform(0, 100);
     auto res = test(n);
     //Prints "float"
     pragma(msg, typeof(res));
}

I expected the result to be real. Why is the return type of test 
inferred as float instead? I would expect it to choose real, as 
both int and float can be converted to real without precision 
loss, but the opposite is not true. Is this a bug?


More information about the Digitalmars-d mailing list