method generation by name

Robert Fraser fraserofthenight at gmail.com
Mon Jul 23 16:40:29 PDT 2007


BCS Wrote:

> What is the best way to generate a method with a given name (as specified 
> by a const char[]) without having to build the whole thing as a string?
> 
> I was hoping this would work:
> 
> |template foo(R, char[] name, A...)
> |{
> |  R mixin(name)(A a)
> |  {
> |    return d(a);
> |  }
> |}
> |
> |mixin foo!(int, "hello", int);
> |
> |void main()
> |{
> |  int i = hello(1);
> |}
> 
> the best I have come up with is a bit hackish and has a few "issues".
> 
> |template foo(R, char[] name, A...)
> |{
> |  R somthing(A a){return R.init;}
> |  mixin("alias somthing "~name~";");
> |}
> 
> If a clean solution for this were made and class level static foreach were 
> to be implemented then auto implementation of an interface would be doable.
> 
> 

I think you might have to use your way until macros come along. What's so wrong with the alias way, though?


More information about the Digitalmars-d-learn mailing list