IDEA: An elegant mechanism for adding an interface to sombody else's code
Russell Lewis
webmaster at villagersonline.com
Mon Aug 27 12:22:19 PDT 2007
I was reading through Walter & Andrei's presentation from the D
conference and ran across the point that one nice reason for making
these two syntaxes equivalent
a.func(b);
func(a,b);
is that it allows you to add "member functions" to basic types.
This gave me an idea for a way to add an interface to a class that
didn't declare it originally:
interface foo {...};
class bar {...};
foo foo(bar b)
{
// generate a temporary wrapper class which implements the
// interface and just forwards all calls to b
return new bar2foo_Wrapper(b);
}
(I bet that somebody could write a template which would convert any
class to any interface, provided that the class implemented the correct
functions!)
Then, anywhere in the code, you could access the "interface" as:
bar b = new bar;
foo f = b.foo;
...which, of course, implies that perhaps that should be the (or a)
standard way to access Interfaces, instead of casts:
interface fred {...}
class wilma : fred {...}
wilma w = new wilma;
fred f = w.fred;
Thoughts?
More information about the Digitalmars-d
mailing list