title: Use Object.factory without needing to cast to a specific type
body: <p>I'm trying to create a similar router to Rails ActionDispatch router, which allows you to define a route similar to </p>
  
  <pre><code>map.get "/foo", :controller =&gt; "Foo", :action =&gt; "index"
  </code></pre>
  
  <p>which will then route <code>GET /foo</code> to <code>FooController#index</code>. With this structure in place you can use methods like</p>
  
  <pre><code>map.resources :foos
  </code></pre>
  
  <p>which will call methods like</p>
  
  <pre><code>map.get "/foo", :controller =&gt; "Foo", :action =&gt; "index"
  map.get "/foo/:id", :controller =&gt; "Foo", :action =&gt; "show"
  </code></pre>
  
  <p>and so on.</p>
  
  <p>In D, I've been able to figure out a lot of the reflexive code required to make this work, but not all of it.  In Ruby I can do:</p>
  
  <pre><code>class Foo
    def bar
      "FOOO BAR!"
    end
  end
  
  f = Object.const_get("Foo")
  f.new.__send__(:bar) #=&gt; "FOOO BAR!"
  </code></pre>
  
  <p>Which I've tried to translate to</p>
  
  <pre><code>module foo;
  import std.stdio;
  
  class Foo {
    void bar() {
      writeln("FOO BAR!");
    }
  }
  
  void main() {
    auto foo = Object.factory("foo.Foo");
    __traits(getMember, foo, "bar");
  }
  </code></pre>
  
  <p>But that doesn't work because the compiler doesn't know what type <code>foo</code> is, so the call to <code>#bar</code> fails during compile.  Everywhere I've seen <code>Object.factory</code> used they cast it to a specific type, so</p>
  
  <pre><code>module foo;
  import std.stdio;
  
  class Foo {
    void bar() {
      writeln("FOO BAR!");
    }
  }
  
  void main() {
    auto foo = cast(Foo) Object.factory("foo.Foo");
    __traits(getMember, foo, "bar");
  }
  </code></pre>
  
  <p>would work just fine. But if I know what I want to cast the object to what good is using <code>Object.factory</code>? That doesn't make <em>any</em> sense to me!</p>
  
  <p><strong>UPDATE 2</strong> I've fixed compiler issues, but now it's crashing at runtime, saying it couldn't find the method</p>
  
  <pre><code>module foo;
  import std.stdio;
  
  class MyDynamic {
    void call(C, T...)(C instance, string method, T args) {
      foreach(member; __traits(allMembers, C)) {
        writeln(member);
        if (member == method) {
          static if (__traits(compiles, __traits(getMember, instance, member)(args))) {
            __traits(getMember, instance, member)(args);
          }
          return;
        }
      }
  
      assert(0, "No method found");
    }
  }
  
  class Foo : MyDynamic {
    void bar() {
      writeln("FOO BAR!");
    }
  }
  
  void main() {
    auto foo = cast(MyDynamic) Object.factory("foo.Foo");
    assert(foo !is null);
  
    foo.call(foo, "bar");
  }
  </code></pre>
  
  <p><strong>Update</strong> For anyone coming to this question now, you can see my final solution here: <a href="https://github.com/jaredonline/action-pack" rel="nofollow">https://github.com/jaredonline/action-pack</a></p>