alias to function literal, call without () not possible

Anonymouse via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 3 11:15:23 PDT 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 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.


More information about the Digitalmars-d-learn mailing list