digitalmars.D.learn - Dynamic code generation
- Rufus Smith (13/13) Jul 19 2016 Does D offer any solutions to generate code dynamically? I would
- ag0aep6g (27/41) Jul 19 2016 Just an idea:
Does D offer any solutions to generate code dynamically? I would like to order based on optimal strategies. This requires effectively hard coding the execution path. A simple example, if (x == true) foo(); else bar(); can be recoded to be foo() or bar() while x is fixed, in my case x is fixed for long periods and therefor the check is unnecessary. I also know when x changes in all cases. This is just a simple example, of course. I would not want to resort to assembly programming to accomplish this.
Jul 19 2016
On 07/20/2016 06:36 AM, Rufus Smith wrote:Does D offer any solutions to generate code dynamically?I don't think so.I would like to order based on optimal strategies. This requires effectively hard coding the execution path. A simple example, if (x == true) foo(); else bar(); can be recoded to be foo() or bar() while x is fixed, in my case x is fixed for long periods and therefor the check is unnecessary. I also know when x changes in all cases. This is just a simple example, of course. I would not want to resort to assembly programming to accomplish this.Just an idea: ---- void main() { setX(true); f(); /* calls foo */ setX(false); f(); /* calls bar */ } void function() f; void setX(bool x) { if (x) f = &impl!true; else f = &impl!false; } void impl(bool x)() { static if (x) foo(); else bar(); } void foo() { import std.stdio; writeln("foo"); } void bar() { import std.stdio; writeln("bar"); } ---- Of course, if this is for optimization purposes, measure first if hard-coded is actually faster than the branch.
Jul 19 2016