jaa has asked for the wisdom of the Perl Monks concerning the following question:
It's been a while since I hit the keys on my codeboard - so apologies if ya'all know all this anyway.
I've recently discovered Object::Pad - and love it. Fingers crossed for some future OO goodness in CORe.
I've kind of forgotten where to ask Perl questions, and posted one on Discord->#perl:
A question on Object::Pad and virtual parent classes.
I want to class Parent :does(RoleA) :does(RoleB) and have class ChildA isa:(Parent); class ChildB isa:(Parent) etc. without having to implement the RoleA and RoleB requires methods inside Parent - in other words, Parent is a virtual class - one that would never be instantiated.
If I do this, it appears that the :does() are evaluated on Parent at compilation rather than instantiation of ChildA or ChildB which actually do implement the requires.
Is there a way to do virtual parent classes?
Kudos to gordonfish for suggesting using role to do this.
So as illustration, you can use role as follows to compose virtual classes.
#!/usr/bin/perl #----------------------------------------------------------------- +------------- use v5.32; use Object::Pad; role Role::FeederThings { requires earnDosh; requires buyKFC; } role Role::TaxiDriver { requires waitForever; requires driveForMiles; } role Role::DadThings :does(Role::FeederThings) :does(Role::TaxiDriver) { } class Parent :does(Role::DadThings) { }
Which results in:
Class Parent does not provide a required method named 'earnDosh' at +./,t1.pl line 25.
Just sharing...
0.02$AU, Jeff.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Object::Pad - Virtual Classes can be composed using Roles.
by kcott (Archbishop) on Jan 08, 2022 at 02:47 UTC | |
by jaa (Friar) on Jan 08, 2022 at 03:26 UTC | |
|
Re: Object::Pad - Virtual Classes can be composed using Roles.
by jaa (Friar) on Jan 09, 2022 at 00:58 UTC |