Return type inference

Alex Rønne Petersen xtzgzorex at gmail.com
Thu May 17 08:34:28 PDT 2012


On 17-05-2012 16:22, Steven Schveighoffer wrote:
> On Thu, 17 May 2012 09:10:18 -0400, Tobias Pankrath
> <tobias at pankrath.net> wrote:
>
>> This does not work and I can see why.
>>
>> ---
>> auto foo(int x)
>> {
>> if(x < 0)
>> return foo(-x);
>> return x;
>> }
>> ----
>>
>> DMD 2.059 says:
>> oopsc/compiler/test.d(7): Error: forward reference to foo
>> oopsc/compiler/test.d(14): Error: forward reference to foo
>>
>> For the human reader it is easy to see that the return type of
>> foo should be int.
>
> At this point, I think the human should intervene:
>
> int foo(int x)
> ...
>
> I would *hate* to have to read code like yours to try and understand it.
>
> -Steve

I have to agree. Sometimes auto can be over-used. Even in functional 
languages with H-M type inference, I tend to always make my functions 
have explicit signatures (unless they're supposed to be generic). It's 
just clearer to the person reading the code later on.

-- 
- Alex


More information about the Digitalmars-d mailing list