digitalmars.D.learn - Mixins: Using the type name to generate a method name
- "Robert Rouse" <robert.e.rouse gmail.com> Feb 21 2012
- "Adam D. Ruppe" <destructionator gmail.com> Feb 21 2012
- Mantis <mail.mantis.88 gmail.com> Feb 21 2012
- "Robert Rouse" <robert.e.rouse gmail.com> Feb 21 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
Feb 21 2012
On Tuesday, 21 February 2012 at 19:42:42 UTC, Robert Rouse wrote:mixin("alias _method " ~ toLower(typeid(T)) ~ ";" );
Try using T.stringof instead of typeid(T). typeid does a runtime lookup. T.stringof does magic to get a string representation of the thing at compile time. Since, in the template, T here is a type, you get a string of that type. .stringof is a bit of magic that can work in a lot of places. last time I checked though, it is pretty poorly documented, so might take some guess and check to make it work for you.
Feb 21 2012
21.02.2012 21:42, Robert Rouse пишет:... mixin("alias _method " ~ toLower(typeid(T)) ~ ";" ); ...
typeid(T).toString(); or typeof(T).stringof; typeid does not return a string type.
Feb 21 2012
stringof did it. I'm still reading through the D programming book, so I guess I hadn't gotten there yet. I did a search in the book and found a reference. I'll read more. Thanks :) On Tuesday, 21 February 2012 at 19:48:18 UTC, Adam D. Ruppe wrote:On Tuesday, 21 February 2012 at 19:42:42 UTC, Robert Rouse wrote:mixin("alias _method " ~ toLower(typeid(T)) ~ ";" );
Try using T.stringof instead of typeid(T). typeid does a runtime lookup. T.stringof does magic to get a string representation of the thing at compile time. Since, in the template, T here is a type, you get a string of that type. .stringof is a bit of magic that can work in a lot of places. last time I checked though, it is pretty poorly documented, so might take some guess and check to make it work for you.
Feb 21 2012









"Adam D. Ruppe" <destructionator gmail.com> 