Minimum value in a range

Timon Gehr timon.gehr at gmx.ch
Thu Aug 4 15:14:36 PDT 2011


Andrei Mitrovic wrote:
> Does anyone know why putting this alias in module scope errors out?:
>
> import std.algorithm;
>
> alias reduce!((a, b){ return 1; }) foo;
>
> void main()
> {
>     foo([1, 2, 3]);
> }
>
> Error: delegate test.__dgliteral1!(int,int).__dgliteral1 is a nested
> function and cannot
> be accessed from reduce
>
> But this will work:
>
> import std.algorithm;
>
> void main()
> {
>     alias reduce!((a, b){ return 1; }) foo;
>     foo([1, 2, 3]);
> }

I believe it is because the function literal is typed as a template delegate and
the compiler cannot figure out where to get the context pointer from (because at
module level, there is no need for a context pointer).
If you declare the alias inside the main function, the 'reduce' function will be
instantiated as a local function and therefore it has the context pointer. The
result is that you pass around some unnecessary context pointers (without
inlining). AFAIK Andrei wants this fixed.

You can explicitly tell the compiler to type the function literal as a function
pointer:

alias reduce!(function (a, b){ return 1; }) foo;

void main()
{
    foo([1, 2, 3]);
}

works.

-Timon


More information about the Digitalmars-d-learn mailing list