www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias to function literal, call without () not possible

reply Andre Pany <andre s-e-a-p.de> writes:
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
next sibling parent reply Anonymouse <asdf asdf.net> writes:
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.
Aug 03 2016
parent reply Andre Pany <andre s-e-a-p.de> writes:
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é
Aug 03 2016
parent ag0aep6g <anonymous example.com> writes:
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
prev sibling parent Kagamin <spam here.lot> writes:
Function pointers and delegates are not intended to allow 
optional parentheses. See also DIP23.
Aug 04 2016