Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 03, 2016 alias to function literal, call without () not possible | ||||
---|---|---|---|---|
| ||||
Hi, I just stumbled over this behavior. I am not sure whether the behavior is correct or not. alias foo = () => new Object; void bar(Object o){} void main() { auto n1 = foo; bar(foo); } While first line in main is working fine, second line does not compile due to missing (). Is this correct? Kind regards André |
August 03, 2016 Re: alias to function literal, call without () not possible | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | On Wednesday, 3 August 2016 at 17:16:10 UTC, Andre Pany wrote: > Hi, > > I just stumbled over this behavior. I am not sure whether > the behavior is correct or not. > [...] > alias foo = () => new Object; ...is an alias for a delegate/function returning an Object. It is analogous to > alias foo = () { return new Object; }; > void bar(Object o){} ...is a function accepting an Object parameter. In main you are trying to call this overload > void bar(Object function() o){} ...which is essentially > void bar(typeof(foo) o) {} You can use pragma(msg, typeof(symbol).stringof) to inspect what types are being thrown around. > void bar(T)(T t) > { > pragma(msg, T.stringof); // "Object function() pure nothrow @safe" > } > > void main() > { > auto n1 = foo; > pragma(msg, typeof(foo).stringof); // "Object function() pure nothrow @safe" > bar(foo); > } You could argue that parameterless function call rules should apply here and have it implicitly converted to bar(foo()), but it doesn't. I imagine the ambiguity (of delegate vs function return value) would just cause more problems than it would solve. |
August 03, 2016 Re: alias to function literal, call without () not possible | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | On Wednesday, 3 August 2016 at 18:15:23 UTC, Anonymouse wrote:
> On Wednesday, 3 August 2016 at 17:16:10 UTC, Andre Pany wrote:
>> [...]
>
>> [...]
> ...is an alias for a delegate/function returning an Object. It is analogous to
>> [...]
>
>> [...]
> ...is a function accepting an Object parameter. In main you are trying to call this overload
>> [...]
> ...which is essentially
>> [...]
>
> You can use pragma(msg, typeof(symbol).stringof) to inspect what types are being thrown around.
>> [...]
>
> You could argue that parameterless function call rules should apply here and have it implicitly converted to bar(foo()), but it doesn't. I imagine the ambiguity (of delegate vs function return value) would just cause more problems than it would solve.
Thanks for the info. Yes, I forgot the () for new Object;
Adding the () for new Object() still returns the same error.
I think you are right with the ambiguity.
Kind regards
André
|
August 04, 2016 Re: alias to function literal, call without () not possible | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | Function pointers and delegates are not intended to allow optional parentheses. See also DIP23. |
August 04, 2016 Re: alias to function literal, call without () not possible | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | On 08/03/2016 09:40 PM, Andre Pany wrote:
> Thanks for the info. Yes, I forgot the () for new Object;
> Adding the () for new Object() still returns the same error.
`new Object` without parentheses is perfectly fine.
|
Copyright © 1999-2021 by the D Language Foundation