Mixin versus c++ preprocessor?
    Tim Burrell 
    tim at timburrell.net
       
    Fri Mar 21 09:37:19 PDT 2008
    
    
  
Yossarian wrote:
> Hello,
> I've got following problem :
> I've used c++ preprocessor for function prototyping, eg.
> 
> #define DO_MY_FUNCTION(name, type) int name(type t, type t2)
> 
> DO_MY_FUNCTION(f1, int) {
> }
> ..
> 
> if you ask why - i got simple interpreted language written, and these  
> prototypes were used
> as internal functions, and the order or types of parameters should vary  
> between versions.
> 
> is there any chance to write code like this in D?
> 
You can using mixins, but it looks ugly, you end up with something like:
template DoMyFunction(char[] Name, char[] Type, char [] Body) {
     const char[] DoMyFunction = "int " ~ Name ~ "(" ~ Type ~ " " ~ Name 
~ ", " ~ Type ~ " " ~ Name ~ ") {" ~ Body ~ "}";
}
mixin(DoMyFunction!("f1", "int", "
	// function body
"));
Mixins are great, but in no way a replacement for the C preprocessor.  I 
think it was a big mistake to not include preprocessing as well.
    
    
More information about the Digitalmars-d-learn
mailing list