develooper Front page | perl.perl6.language.objects | Postings from October 2000

Re: RFC 265 (v3) Interface polymorphism considered lovely

Thread Previous
From:
Piers Cawley
Date:
October 6, 2000 07:52
Subject:
Re: RFC 265 (v3) Interface polymorphism considered lovely
Message ID:
m1aeci58n9.fsf@rt158.private.realtime.co.uk
Perl6 RFC Librarian <perl6-rfc@perl.org> 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?)


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About