correct way to create boiler plate code
Timon Gehr
timon.gehr at gmx.ch
Mon May 16 12:35:23 PDT 2011
> I am parsing some formulas from a spreadsheet file. To duplicate
> the behavior of the spreadsheet functions, I am having to create a
> lot of boiler plate code that maps from the spreadsheet functions
> to the built-in functions. Mixin would seem to allow me to
> automate the boiler-plate creation, but i am not utilizing it
> correctly. What is the correct to call Mixin in this situation, or
> is their a better way to create the boiler-plate code?
>
> For background, i am new to D. D is my second language with my
> primary experience being a script kiddie in Excel VBA, so take it
> slow, please.
hi,
Disregard the other answer, making func immutable won't solve your problem.
> string[] funcs = ["tan"];
> void createFuncs(){
> foreach(func; funcs){
> mixin("double " ~ toupper(func) ~ "(double aReal)
> {return(" ~ func ~ "(aReal));}");
> }
> }
foreach runs at runtime, while mixin is expanded at compile time.
If you want to inject your boilerplate into module namespace, you would rather
write a function that generates the code and mixin the result of a call to that
function:
string[] funcs = ["tan"];
string createFuncs(){
string res;
foreach(func; funcs){
res~="double " ~ toupper(func) ~ "(double aReal){return(" ~ func ~ "(aReal));}";
}
return res;
}
mixin(createFuncs());
More information about the Digitalmars-d-learn
mailing list