enum function can't be passed into template?

Timon Gehr timon.gehr at gmx.ch
Mon Jan 21 01:33:47 PST 2013


On 01/20/2013 03:21 PM, Zhenya wrote:
> Hi!
> Am I doing something wrong?
>
> import std.stdio;
>
> template gun(alias f)
> {
>      void gun()
>      {
>          f();
>      }
> }
>
> void main()
> {
>      auto str = "hello";
>      enum fun = (){writeln(str);};//replace enum -> auto to compile
>      gun!fun();
> }
>
> Error:delegate c634.main.__lambda1 is a nested function and cannot be
> accessed from c634.gun!(delegate @system void()
> {
> writeln(str);
> }
> ).gun

I'd say it is a compiler bug. The following compiles and runs:

import std.stdio;

template gun(alias f, alias g){
     void gun(){
         f();
     }
}

void main(){
     int dummy;
     auto str="hello";
     enum fun=(){writeln(str);};
     gun!(fun,dummy)();
}

The issue is that 'fun' alone is treated as static by the compiler and 
therefore does not cause 'gun' to be instantiated locally inside main.

It is now a matter of whether a closure can be stored inside a local 
enum, which the spec is silent about. I think it should work. The 
compiler is in error in both cases.


More information about the Digitalmars-d-learn mailing list