On Thursday, 1 November 2012 at 15:59:04 UTC, Gor Gyolchanyan
wrote:
OR, better yet:
mixin MyMixin
{
foreach(i; 0..10)
MyMixin ~= "writeln(" ~ to!string(i); ~ ");\n"'
}
And this could be printed out as a pragma(msg, ...) or into a
.log file or
anywhere else. This way it's easy to see what did end up being
mixed in.
Given that template mixins are currently declared as (mixin
template Foo () {...}), would it make even more sense to extend
this out to a more literal interpretation of the subject line
("mixin functions")? I'm thinking something like this:
mixin string myMixin () {
string res;
foreach ( i ; 0 .. 10 )
res ~= "writeln(" ~ to!string( i ) ~ ");\n";
return res;
}
And later using it as:
mixin myMixin();
The same as templates. The caveat is that the compiler treats
'myMixin' as though it were a template containing only the
function, so it is evaluated in the context of where it is mixed
in. Since it makes little sense to return anything other than a
string, perhaps the absence of the 'template' keyword would imply
a function with 'string' return type.
mixin myMixin () {
// ...same...
}
The mixin *expression*, of course, would be unaffected. Maybe it
would be of limited benefit to the OP, but it might be generally
useful. Otherwise, while ugly, the current anonymous delegate
approach isn't the most horrid thing imaginable.
-- Chris Nicholson-Sauls