Wrapping instance methods
Earlier, we looked at a dynamic type in D which included the capability to wrap native functions with dynamic typing and property replacement. Now, it is time to see exactly how that works and how automated code generation makes it a lot easier.
Note
By unifying types, we enable both dynamic transformations as well as create an array of delegates to access functions of varying types. We cannot declare an array of function pointers that all take different arguments, but we can declare an array of pointers to helper functions that take one input array and transform the values for use.
How to do it…
Let's execute the following steps to wrap instance methods:
Write a generic conversion function to and from your consistent type. You should use
std.variant.Variant
orstd.conv.to
directly, if possible, and use type families withstd.traits
if working with a custom type.Write a helper function generator that takes an existing function as a runtime delegate and returns the wrapped...