<HTML><HEAD><META content="text/html; charset=UTF-8" http-equiv="Content-Type"></HEAD><BODY>Awesome. Thanks!<BR>
<BR>
Adam D. Ruppe wrote:<BR>
<blockquote type="cite"><BR>
On Tuesday, 21 February 2012 at 14:53:06 UTC, Robert Rouse wrote:<BR>
<blockquote type="cite"><BR>
Using a mixin, is it possible to have it define a method based on a <BR>
string passed into the mixin?<BR>
</blockquote><BR>
<BR>
Yeah, though you'll have to build a string of the method.<BR>
<BR>
Something like this:<BR>
<BR>
string make_method_string(string name) {<BR>
string code = "void "; // return type<BR>
code ~= name;<BR>
code ~= "() {"; // arglist<BR>
code ~= q{ // body (q{ } is just another string literal but prettier <BR>
for this imo<BR>
writeln("hello");<BR>
};<BR>
code ~= "}";<BR>
return code;<BR>
}<BR>
<BR>
mixin template make_method(string name) {<BR>
mixin(make_method_string(name)); // string mixin runs the above <BR>
function at compile time, and then pastes the resulting code in here <BR>
to be compiled<BR>
}<BR>
<BR>
<BR>
// and now to use it<BR>
<BR>
struct Test {<BR>
mixin make_method("bark");<BR>
}<BR>
<BR>
<BR>
void main() {<BR>
Test foo;<BR>
foo.bark(); // works!<BR>
}<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
The string mixin and string builder helper function pattern<BR>
can do pretty much anything you can think up, though it can<BR>
get pretty ugly as it gets bigger just due to being strings.</BODY></HTML>