Attribute inference for auto functions?

Peter Alexander peter.alexander.au at gmail.com
Wed Apr 17 06:59:03 PDT 2013


On Wednesday, 17 April 2013 at 12:46:53 UTC, Andrej Mitrovic 
wrote:
> On 4/17/13, Peter Alexander <peter.alexander.au at gmail.com> 
> wrote:
>> Type inference happens if and only if you do
>> not specify a return type.
>
> That's not true, you still need to use auto. This function 
> definition
> is illegal:
>
> x() { return 1; }

You don't need to use auto. Any storage class will do.


> As for being able to use 'const' for type inference it's 
> completely an
> implementation issue. It's not by design at all. You can even
> substitute 'auto' for meaningless crap like this in module 
> scope:
>
> static x() { return 1; }
>
> If return type inference is wanted then 'auto' should be 
> required at
> all times. What's the point in writing obfuscated code like the 
> above?

Because it's required?

This is illegal:

auto const foo();

If you want type inference then you just need to use:

const foo();

And yes, it is by design. The spec is quite clear on this issue. 
Type inference is signalled by lack of return type -- not the 
presence of auto. auto is only required when no other storage 
class is wanted, to make the parser happy. auto is nothing 
whatsoever to do with type inference.


More information about the Digitalmars-d mailing list