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.
  • Dynamic code generation Rufus Smith via Digitalmars-d-learn
    • Re: Dynamic code generation ag0aep6g via Digitalmars-d-learn

Reply via email to