Thread overview
enum function can't be passed into template?
Jan 20, 2013
Zhenya
Jan 20, 2013
Philippe Sigaud
Jan 20, 2013
Zhenya
Jan 20, 2013
Philippe Sigaud
Jan 21, 2013
Timon Gehr
January 20, 2013
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
January 20, 2013
On Sun, Jan 20, 2013 at 3:21 PM, Zhenya <zheny@list.ru> 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();
> }

fun depends on str, which is a runtime value. Either make str an enum or put them in the module scope (which will make the auto's enum's)
January 20, 2013
On Sunday, 20 January 2013 at 14:51:51 UTC, Philippe Sigaud wrote:
> On Sun, Jan 20, 2013 at 3:21 PM, Zhenya <zheny@list.ru> 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();
>> }
>
> fun depends on str, which is a runtime value. Either make str an enum
> or put them in the module scope (which will make the auto's enum's)
Thank you!
January 20, 2013
>>> void main()
>>> {
>>>         auto str = "hello";
>>>         enum fun = (){writeln(str);};//replace enum -> auto to compile
>>>         gun!fun();
>>> }
>>
>>
>> fun depends on str, which is a runtime value. Either make str an enum or put them in the module scope (which will make the auto's enum's)
>
> Thank you!

Which, by the way, does not explain why auto/auto works...
January 21, 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.