I would like to be able to define a function that accepts an interface, but can be executed using a delegate or functions that provide the same functionality. For example, in C ++, I can write something like:
typedef std::function<int (float)> toInt; void fun(toInt dg) { ... } struct impl1 { int operator()(float x) { ... } }; int impl2(float x) { ... }
And then call it using any implementation:
fun(impl1()); fun(&impl2);
(This conversion float-> int is just a simplified example illustrating the principle, not my actual functionality).
I would like to achieve something similar in D. My naive attempt was this:
interface toInt { int opCall(float); } void fun(toInt dg) { ... } int impl2(float x) { ... } fun(impl2);
The compiler complains about this last line that it cannot implicitly convert impl2 to type toInt. Maybe I'll just add an overloaded implementation of pleasure and make the conversion explicit, but I was wondering if there was a more elegant and general way to do this, as in the C ++ example above.
interface delegates d d2
Eitan
source share