Thanks tobyink for this, I found it very interesting!
I learned OO on Java, then graduated to C++. So, my first thought on reading the above was: roles here are analogous to Java interfaces — except that a role contains an implementation whereas an interface does not. But then I wondered: how is this different from good old (possibly multiple) inheritance? I changed your first example by removing the references to Role::Tiny and replacing:
use Role::Tiny::With; with "Roles::Jumper";
with:
use base qw( Roles::Jumper );
in both the Pet::Dog and Pet::Cat classes. The resulting output was the same. So, my first question is: what advantage is there in using Role::Tiny over using simple, single inheritance here?
My second question arises from this statement:
When there's a clear "is-a" relationship between the classes..., and when the base class ... is potentially useful by itself, then use inheritance. Otherwise, use roles.
Can you explain the reasoning here? I’m still trying to get my head around Perl’s approach to OO. In my present state of ignorance, the two techniques you illustrate look like syntactic variations on an identical theme, and I can’t see any reason to prefer one over the other.
Thanks,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re^2: OO: how to make a generic sub (use roles or inheritance)
by Athanasius
in thread OO: how to make a generic sub
by Hossein
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |