How to mixin finction name?

Adam D. Ruppe destructionator at gmail.com
Sun Apr 14 13:22:38 UTC 2019


On Sunday, 14 April 2019 at 12:00:38 UTC, Andrey wrote:
> I want to mixin only name - not the full function code.

You can't. Best you can do is write the function separately and 
then mixin an alias for it with the other name.

void main()
{
     enum letters = ['A', 'B', 'C'];

     // normal implementation, parameterized via template
     void printImplementation(char ch)(uint i) {
         import std.stdio;
         writeln(ch, " - ", i);
     }

     static foreach(ch; letters)
     {
         // mixin the name separately
         mixin("alias print" ~ ch ~ " = printImplementation!ch;");
     }

     printB(6);
}


Though, I'd point out the mixin code doesn't have to be too ugly. 
Consider this:

void main()
{
     enum letters = ['A', 'B', 'C'];

     static foreach(ch; letters)
     {
     	mixin(q{
		void print}~ch~q{(int i) {
		    import std.stdio;
		    writeln(ch, " - ", i);
		}
	});
     }

     printB(6);
}


Well, the name part is a big ugly, but the rest of it looks 
perfectly normal, and the compiler error message still give 
reasonable results.


More information about the Digitalmars-d-learn mailing list