Compile time only delegates

Frustrated Frustrated at nowhere.com
Tue Mar 25 13:22:38 PDT 2014


On Tuesday, 25 March 2014 at 20:20:29 UTC, Frustrated wrote:
> On Tuesday, 25 March 2014 at 18:10:17 UTC, Meta wrote:
>> On Tuesday, 25 March 2014 at 18:05:47 UTC, Frustrated wrote:
>>> Due to a previous issue I am trying to do the following
>>>
>>> mixin template A()
>>> {
>>>   mixin((function () => "int x;")() );
>>> }
>>>
>>> the problem is that the compiler will not let me use the 
>>> delegate because it has no this context. Of course the whole 
>>> point here is that the delegate will never be used at runtime 
>>> since it is used in the mixin(which is purely compile time).
>>>
>>> It should work(there is no reason it shouldn't) yet it 
>>> doesn't.
>>>
>>> The goal is that I do not want to create a function outside 
>>> the mixin to use because it will be included in the context 
>>> of the template mixin. e.g.,
>>>
>>> mixin template A()
>>> {
>>>   string B() { return "int x;"; }
>>>   mixin(B()); // still doesn't work but if it did B would be 
>>> inserted in the context of the template. e.g., if used in a 
>>> class the class would then have a method named B in it, hence 
>>> the use of the lambda.
>>> }
>>
>> There shouldn't be a context pointer at all as you specified 
>> that it's a function, not a delegate. Function literals don't 
>> have context pointers as far as I know. Did you try:
>>
>> mixin((function () { return "int x;" })());
>>
>> Just to be sure?
>
> Thanks, I thought I did try that(that was the first thing I 
> tried I thought). Seems to be working though.

oops, sorry, same problem. When used inside a class says I need 
this:

Heres the actual code:



import std.stdio, std.cstream;

// mixin template used to wrap the string mixin
mixin template C()
{
	mixin((function () { return "void foo(iA a) { 
writeln(`Generic`);  }"; })());
}

// eponymously named template used as a string mixin to generate 
the function
template B()
{
	string B() { return "void foo(iA a) { writeln(`Generic`);  }"; }
}

interface iA {	void foo(iA a); }

class A : iA
{
	void foo(A a) { writeln("Specific"); }
	//mixin(B);
	mixin C;    // doesn't work, use line above (mixin templates 
won't override even if different parameters)
}


void main()
{
	
	iA a = new A;
	a.foo(a);					// Generic/Generic
	(cast(A)a).foo(cast(A)a);	// Specific/Specific
	a.foo(cast(A)a);			// Generic/Specific
	(cast(A)a).foo(a);			// Specific/Generic     line doesn't work 
when using mixin template
	
	writeln("-----");
	mixin C;
	foo(a);

	din.getc();
}



and the error:

Error: function main.A.C!().__funcliteral1 need 'this' to access 
member __funcliteral1	

(this only happens using the mixin C in the class, not in main or 
when using mixin B)





More information about the Digitalmars-d-learn mailing list