Implementing required methods with alias
Reiner Pope
some at address.com
Tue Jul 24 01:00:04 PDT 2007
I've been having some fun with the 2.003 __traits, trying to make a
template which wraps a class in a "thunk" so that it conforms to a given
interface. The idea is to allow:
interface Foo
{
void run();
}
...
class Bar // doesn't inherit from Foo
{
void run() {...} // but implements run anyway
}
...
Bar b = ... ;
Foo f = CreateWrapper!(Foo)(b);
f.run(); // calls b.run()
The problem I ran into was the inability to use aliases to implement the
required methods of an interface. For example, the following doesn't work:
interface Bar
{
void run();
}
class Bam : Bar
{
void _run() {}
alias _run run;
}
it errors at compile-time with "Bar.run isn't implemented."
I think it would make sense if this worked, and it would be useful for
the wrapper I mentioned above, because it allows me to generate the
methods in a separate namespace (with template mixins) and pull them in
neatly with aliases.
Is there a reason to desire the current behaviour?
While we're at it, I thought it could also be nice to extend the alias
behaviour to support the following:
interface Foo
{
void run();
}
interface Bar
{
void run();
}
class Bam : Foo, Bar
{
void run1() {...}
alias run1 Foo.run;
void run2() {...}
alias run2 Bar.run;
}
In this case, the aliases are specifically saying, "use this alias to
implement Foo.run or Bar.run," which I think is a powerful distinction
to be able to make.
What do you think?
-- Reiner
More information about the Digitalmars-d
mailing list