in reply to Why do you need abstract classes in Perl?
One thing that distinguishes an "abstract" class from a concrete one is that you shouldn't create instances the abstract class. There's no need for special syntax for this. You need merely do something like:
And then rely on subclasses to implement their own new. Unfortunately, this confounds the common (and useful) idiom of inheriting new from a (possibly abstract) superclass.package AbstactFoo; ... sub new { die "Don't create any Abstract Foos direct, fool!"; }
Here's a way to get the benifit of an "abstract" new while still keeping the class abstract:package AbstractFoo; ... sub new { my $package = shift; bless { @_ }, $package; } ... my $foo1 = new SubclassOfAbstractFoo(); # GOOD my $foo2 = new AbstractFoo(); # BAD
package AbstractFoo; ... sub new { my $package = shift; die "Don't subclass AbstractFoo directly!" if $package eq "AbstractFoo"; bless { @_ }, $package; }
(Bonus points to whoever sees a way to generalize this even further.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Why do you need abstract classes in Perl?
by merlyn (Sage) on Mar 05, 2001 at 07:52 UTC | |
by dws (Chancellor) on Mar 05, 2001 at 10:41 UTC | |
by merlyn (Sage) on Mar 05, 2001 at 12:01 UTC |