digitalmars.D.learn - alias to function literal, call without () not possible
- Andre Pany (15/15) Aug 03 2016 Hi,
- Anonymouse (12/33) Aug 03 2016 ...is an alias for a delegate/function returning an Object. It is
- Andre Pany (6/26) Aug 03 2016 Thanks for the info. Yes, I forgot the () for new Object;
- ag0aep6g (2/4) Aug 04 2016 `new Object` without parentheses is perfectly fine.
- Kagamin (2/2) Aug 04 2016 Function pointers and delegates are not intended to allow
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é
Aug 03 2016
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 toalias foo = () { return new Object; };void bar(Object o){}...is a function accepting an Object parameter. In main you are trying to call this overloadvoid bar(Object function() o){}...which is essentiallyvoid 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.
Aug 03 2016
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: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é[...][...]...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.
Aug 03 2016
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.
Aug 04 2016
Function pointers and delegates are not intended to allow optional parentheses. See also DIP23.
Aug 04 2016