[Issue 6022] auto return type inference could be improved

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Aug 27 14:51:05 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=6022

Andrej Mitrovic <andrej.mitrovich at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |WONTFIX

--- Comment #2 from Andrej Mitrovic <andrej.mitrovich at gmail.com> ---
Hmm actually it's better if this is explicit. Otherwise the compiler may end up
changing the return type if it finds a better type which all the return
expressions implicitly convert to.

For example:

-----
auto foo()
{
   if (0)
   {
       return [long.max];
   }
   else
   {
       return [int.max + 1];
   }
}
-----

Currently it's an error. In theory the compiler could decide to make the return
type `long[]`, that would make the second array contain a positive long
integer. 

*But*, if you remove the first expression the compiler will infer the return
type as `int[]` and the second array will contain a negative number (due to
signed int wraparound).

I feel it would be a little dangerous to have such implicit type deduction
matching in place. I'm marking my enhancement request as wontfix.

--


More information about the Digitalmars-d-bugs mailing list