Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way I do this is to build my own factory function and dynamic dispatches. Using __traits(allMembers), loop through all the supported classes and get a list of methods. Write a wrapper template that takes generic arguments and converts them to the arguments the function needs. Store a reference to the wrapper function in an associative array, or have a dispatch method in your interface to call it.</p> <p>When it is time to do the work, create the class (with your own wrapper, or you could also use Object.factory and cast it to some generic interface with your dynamic dispatch function), then use the dynamic function. So something like:</p> <pre><code>// IMPORTANT: Object.factory needs a full name - includes the module and class name! auto foo = cast(MyDynamic) Object.factory("mymodule.Foo"); assert(foo !is null); // Object.factory can return null if it didn't find the class // and cast can also return null if it wasn't actually of that interface type, so gotta check foo.call("my_method", ["arg", "arg2", ...]); </code></pre> <p>I updated this link with a full example, refresh if you don't see <code>module dynamicstuff;</code> at the top:</p> <p><a href="http://arsdnet.net/dcode/test46.d" rel="noreferrer">http://arsdnet.net/dcode/test46.d</a></p> <p>loop allMembers, invoke based on a runtime string. Getting a list of all classes that implement an interface is possible too, by looping over ModuleInfo. See the bottom of the example file for a function to do it.</p> <p>My web.d does this to do automatic calling of functions from the web. Long, messy code, but it does a lot. Here's the wrapper function: <a href="https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/web.d#L2538" rel="noreferrer">https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/web.d#L2538</a></p> <p>Note the use of ParameterTypeTuple!func from std.traits.</p> <p>I put a lot of comments in here <a href="http://arsdnet.net/dcode/test46.d" rel="noreferrer">http://arsdnet.net/dcode/test46.d</a> so hopefully they will answer your questions. That example briefly demonstrates:</p> <ul> <li>compile time reflection with __traits (MyDynamicImplementation)</li> <li>run time reflection with ModuleInfo and ClassInfo (getAllDynamicClasses)</li> <li>User-defined attributes (isDynamicallyAvailable)</li> <li>Calling a method with dynamic data (MyDynamicImplementation, uses ReturnType, to, ParameterTypeTuple, and there's commented code for Variant if you are interested)</li> <li>An alternative to multiple inheritance, using an interface and mixin template together.</li> </ul> <p>You don't necessarily have to use all that stuff, but I figured I'd touch upon all of it since these can all be pretty useful for these url routing tasks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload