more useful options | |
PerlMonks |
Re: Interfacesby Dominus (Parson) |
on Dec 01, 2000 at 04:28 UTC ( [id://44271]=note: print w/replies, xml ) | Need Help?? |
[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:
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: Here's the Fish I used: 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 Section
Seekers of Perl Wisdom
|
|