www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - enum function can't be passed into template?

reply "Zhenya" <zheny list.ru> writes:
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
Jan 20 2013
next sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
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)
Jan 20 2013
parent reply "Zhenya" <zheny list.ru> writes:
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!
Jan 20 2013
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
 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...
Jan 20 2013
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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.
Jan 21 2013