Mixins: Using the type name to generate a method name

Robert Rouse robert.e.rouse at gmail.com
Tue Feb 21 11:42:41 PST 2012


Piggy backing on my other question.

I want to be able to make the name of the method optional in the 
argument list. If it doesn't exist, it should get the type name 
of the passed in type and lower case it and use it instead.

I tried the following

import std.stdio, std.string;


mixin template make_method(T, string name = null) {

   void _method() {
     writeln("I am a banana");
   }

   static if(name) {
     mixin("alias _method " ~ name ~ ";" );
   }
   else {
     mixin("alias _method " ~ toLower(typeid(T)) ~ ";" );
   }
}


class Test {
   mixin make_method!(Test);
}



void main(string[] args) {
   Test test;
   test = new Test();
   test.test();
}

The compiler throws

mixtest.d(14): Error: template std.string.toLower(S) if 
(isSomeString!(S)) does not match any function template 
declaration
mixtest.d(14): Error: template std.string.toLower(S) if 
(isSomeString!(S)) cannot deduce template function from argument 
types !()(TypeInfo_Class)
mixtest.d(14): Error: argument to mixin must be a string, not 
("alias _method " ~ (__error) ~ ";")
mixtest.d(26): Error: mixin mixtest.Test.make_method!(Test) error 
instantiating


Am I just SOL on this one and I have to pass the name all the 
time or is there a way to make this work?

Thanks


More information about the Digitalmars-d-learn mailing list