Function literals can't be class members

JS js.mdnq at gmail.com
Tue Jul 9 04:03:33 PDT 2013


On Tuesday, 9 July 2013 at 10:25:26 UTC, John Colvin wrote:
> On Monday, 8 July 2013 at 22:41:41 UTC, JS wrote:
>> trying to use a lambda inside a sub template gives an error.
>>
>> mixin template a
>> {
>>   template b()
>>   {
>>      enum b = { }();
>>   }
>>   mixin(b!());
>> }
>>
>> gives the error in the subject, removing the nesting or using 
>> a function instead of a lamda produces no error, yet all are 
>> essentially semantically equivalent... just the lamda version 
>> requires less typing. (in fact, it would be nice to have a 
>> simplification of template b() = { ... }();)
>
> This belongs in D.learn.
>
> The code you provided is never going to give any "Function 
> literals can't be class members" error, although it does have a 
> lot of other things wrong with it.
>
> 1) The mixin template has no parameter list.
> 2) enum b = { }(); isn't valid without a function body. Did you 
> mean (){} ?
> 3) and it definitely isn't going to return a string to work 
> with mixin();
>
> It helps to give code examples that are at least syntactically 
> correct, if not immediately compilable. A correct and 
> simplified test case of what I *think* you are talking about is 
> this:
>
> mixin template A()
> {
> 	auto foo = (){ };
> }
>
> class C
> {
> 	mixin A!();
> }
>
> Error: delegate f977.C.A!().__lambda1 function literals cannot 
> be class members
>
> Lambda function and delegate literals cannot be class members. 
> Function literals can. I'm not entirely sure on the reason why, 
> it's probably to do with context pointers.

Maybe you need to go tell Artur that he is wrong first before you 
bitch at me for being wrong... 
http://forum.dlang.org/thread/myalrxrrwoljxbboaymh@forum.dlang.org


Please, at least if you are going to bitch and talk down to me 
about writing valid source code at least know what you are 
talking about in the first place...


module main;

import std.stdio, std.cstream;

template b()
{
	enum b = { return "\"asdf\"";  }();
	pragma(msg, b);
}

int main(string[] argv)
{
	auto f = (){ writeln("asd"); };
	f();
	writeln(mixin(b!()));
	din.getc();
     return 0;
}



More information about the Digitalmars-d mailing list