[oops, this is actually an answer to the very similar question posed in this thread. It's pretty much the same question, but the poster wants the checking for missing methods to occur at compile time instead of at run time (or not at all.) I hope this explanation makes clearer what problem I'm trying to solve and why I needed more code than the simpler solutions shown above]

Well, I have an idea, but I've never really tried it out, so I don't know how practical it is. My idea is that the abstract base class can keep track of who is derived from it, and have an INIT block that checks to make sure all its derived classes define the appropriate methods. The INIT block is called after compilation is complete, but before program execution begins. A test implementation looked reasonable:

package Abstract; use Carp; my @inheritors; sub import { my $caller = caller; push @inheritors, $caller; } my @abstract_methods = qw(swim fly); sub INIT { my $bad = 0; for my $class (@inheritors) { for my $meth (@abstract_methods) { no strict 'refs'; unless (defined &{"${class}::$meth"}) { $bad=1; warn "Class $class inherits from Abstract, but does not define + $meth.\n"; } } } croak "Compilation aborted" if $bad; } 1;
Abstract wants its subclasses to define swim and fly methods. If you define a class, say Fish, which inherits from Abstract and defines a swim method but no fly method, you get a fatal error at compile time:
Class Fish inherits from Abstract, but does not define fly. Compilation aborted at fish.pl line 0
Here's the Fish I used:
package Fish; use Abstract; sub swim { "bloop bloop bloop"; } 1;
Then test with perl -e 'use Fish'.

There are some problems with this implementation. For example, you might want some way to derive less-abstract classes from the abstract base class, and this implementation doesn't allow that. But I think the basic idea is sound.

The other thing that came to mind is that Damian Conway probably has something interesting to say about this. Have you checked his book?


In reply to Re: Interfaces by Dominus
in thread Interfaces by haggs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.