Perl6 RFC Librarian <[EMAIL PROTECTED]> writes:
> This and other RFCs are available on the web at
> http://dev.perl.org/rfc/
>
> =head1 TITLE
>
> Interface polymorphism considered lovely
>
> [...]
>
>
> =head1 IMPLEMENTATION
>
> I have some ideas about this, and I believe it may be possible to do a
> good chunk of the runtime/compile time behaviour in pure perl, but if
> we are to gain performance gains for more than just the programmer
> (though it's my contention that the potential time savings available to
> the programmer who's bug hunting are reason enough for this change)
> this stuff should be usable in core. And I know little of the core.
Right, I've done more than think, I've implemented (but not yet
written an exhaustive test suite or docs...). Check out
http://www.well.com/user/pdcawley/perl/Interface-Polymorphism-0.1.tar.gz
which I'll hopefully be putting in CPAN RSN.
There are two modules ex::interface and ex::implements (The ex::
namespace was suggested by Tim Bunce for experimental pragmas) and
work as follows:
package Pet;
use interface qw/eat speak defecate/;
package Dog;
use implements Pet;
sub eat {...}
If the above is the complete definition of Dog then compilation will
fail with a list of the methods that haven't yet been implemented. If
a class implements more than one interface then the error report will
include a listing of all missing methods in all packages and
interfaces.
If you don't want compile time checking of of interfaces you can just
do
use base Pet;
And you'll get runtime error reporting via the interface's AUTOLOAD.
For how it all works look at the code. The workings are much simpler
than I expected, whilst still not being entirely clear. Initial
attempts however were *way* more complex. (Have you ever tried
exporting and 'import' routine into the 'use'ing package?)