digitalmars.D.learn - enum function can't be passed into template?
- Zhenya (22/22) Jan 20 2013 Hi!
- Philippe Sigaud (3/19) Jan 20 2013 fun depends on str, which is a runtime value. Either make str an enum
- Zhenya (2/27) Jan 20 2013 Thank you!
- Philippe Sigaud (1/12) Jan 20 2013 Which, by the way, does not explain why auto/auto works...
- Timon Gehr (19/41) Jan 21 2013 I'd say it is a compiler bug. The following compiles and runs:
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
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
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:Thank you!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
Which, by the way, does not explain why auto/auto works...Thank you!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
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); } ).gunI'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