| Thread overview | |||||
|---|---|---|---|---|---|
|
September 10, 2012 Re: Allow auto in function parameters with default arguments? | ||||
|---|---|---|---|---|
| ||||
On 09/10/12 06:20, Andrej Mitrovic wrote:
> It occurred to me that using a parameter with a default value that is a function call could benefit from using auto:
>
> struct Foo(T) { }
>
> auto getFoo()
> {
> return Foo!int();
> }
>
> void func(int x, auto foo = getFoo()) { }
>
> Granted this is a simple case and might be overkill, but if the function returns some complicated range type (or worse, a Voldemort type) it might be hard or impossible to specify the type.
void func(int x, typeof(getFoo()) foo = getFoo()) { }
But 'auto' is already allowed for function return types and that is a trickier case (you need function bodies to figure out the type) so making it also work for arguments shouldn't be a problem (language-wise).
void func(int x, const foo = getFoo()) { }
etc would then also work.
artur
| ||||
September 10, 2012 Re: Allow auto in function parameters with default arguments? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | On 09/10/2012 12:34 PM, Artur Skawina wrote: > On 09/10/12 06:20, Andrej Mitrovic wrote: >> It occurred to me that using a parameter with a default value that is >> a function call could benefit from using auto: >> >> struct Foo(T) { } >> >> auto getFoo() >> { >> return Foo!int(); >> } >> >> void func(int x, auto foo = getFoo()) { } >> >> Granted this is a simple case and might be overkill, but if the >> function returns some complicated range type (or worse, a Voldemort >> type) it might be hard or impossible to specify the type. > > void func(int x, typeof(getFoo()) foo = getFoo()) { } > > But 'auto' is already allowed for function return types and that is a > trickier case (you need function bodies to figure out the type) so > making it also work for arguments shouldn't be a problem (language-wise). > It is a mere grammar issue. I assume the analyzer is already able to deal with it. > void func(int x, const foo = getFoo()) { } > > etc would then also work. > > artur > struct foo{ } | |||
September 10, 2012 Re: Allow auto in function parameters with default arguments? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On 09/10/12 15:46, Timon Gehr wrote:
> On 09/10/2012 12:34 PM, Artur Skawina wrote:
>> On 09/10/12 06:20, Andrej Mitrovic wrote:
>>> It occurred to me that using a parameter with a default value that is a function call could benefit from using auto:
>>>
>>> struct Foo(T) { }
>>>
>>> auto getFoo()
>>> {
>>> return Foo!int();
>>> }
>>>
>>> void func(int x, auto foo = getFoo()) { }
>>>
>>> Granted this is a simple case and might be overkill, but if the function returns some complicated range type (or worse, a Voldemort type) it might be hard or impossible to specify the type.
>>
>> void func(int x, typeof(getFoo()) foo = getFoo()) { }
>>
>> But 'auto' is already allowed for function return types and that is a trickier case (you need function bodies to figure out the type) so making it also work for arguments shouldn't be a problem (language-wise).
>>
>
> It is a mere grammar issue. I assume the analyzer is already able to deal with it.
>
>> void func(int x, const foo = getFoo()) { }
>>
>> etc would then also work.
>
> struct foo{
>
> }
Well, that might be an argument against anonymous function parameters. Requiring the 'auto' keyword here does not seem right.
artur
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply