Lazy mixins

Frustrated Frustrated at nowhere.com
Tue Mar 25 14:17:49 PDT 2014


On Tuesday, 25 March 2014 at 20:23:47 UTC, Philippe Sigaud wrote:
> That's probably not the solution you want, but could you use 
> direct
> string mixins?
>
>
> import std.stdio;
>
>
> string B() { return `void foo(double d) { 
> writeln("foo(double)"); }`; }
>
> class C
> {
>     void foo(int x) { writeln("x"); }
>     mixin(B());
> }
>
> void main() {
>     auto c = new C();
>     c.foo(1.3);
> }

No, I think I mentioned that string mixins can't get the context 
they are inserted in. Why would I simply wrap a string mixin 
around a template mixin if there wasn't some purpose that string 
mixins couldn't use in the first place?

For example, with a template mixin you can do this

mixin A;

while with a string mixin you have to do

mixin B!(typeof(this));

because A has the context of what it is inserted an you can use 
typeof(this) and it refers to the proper context. In B, there is 
no such context so it has to be passed requiring the user to 
always pass it. Hence string mixins can't do everything. The 
problem with template mixins is that they don't properly 
overload(only use the name to check, if it exists then it won't 
insert/overload).

Hence you can't do both. Use string mixins and you got to supply 
the argument. Use template mixins and you can't overload 
properly(which makes it useless in my case). If one could 
evaluate the string mixin after the template mixin then it would 
all work. (and yes, it is the solution I want unless you can 
actually come up with something that solves the problem 
directly(both overloads properly and allows one to get the 
context)

One can't do something like

template B(T = typeof(this))
{

}


which would also solve the problem(but string mixins don't 
inherit scope so it won't work).



More information about the Digitalmars-d mailing list