user-pic

mishin

Subscribe to feed Recent Actions from mishin

  • autarch.urth.org commented on Are roles only semantically different from inheritance?

    They're really not the same, though they are both ways of implementing polymorphism.

    First of all, roles are composed into classes. This means that all of a role's methods end up in the class as if they'd been defined there. There's no need to ever call "SUPER::foo". When a class consumes multiple roles, method conflicts can be detected at compile time and the role mechanism can throw an error. This forces developers to manually resolve the conflict, unlike subclassing. I suppose in theory you could write a subclassing mechinism like use parent that also did this, but wh…

  • Zak B. Elep commented on Are roles only semantically different from inheritance?

    My take is that roles/traits provide "horizontal" composition of state and behaviors across (possibly unrelated) classes, while inheritance is a "vertical" composition between parent and child.

  • http://www.wgz.org/chromatic/ commented on Are roles only semantically different from inheritance?

    You're missing out on the allomorphic behavior of roles.

    Consider the non-inheritance relationship, where one object delegates to another. You can't get the right answer about a type question by asking "Does the delegator inherit from the delegatee?", and you know that if you limit all of your questions about object behavior to "Does this object have a method of this name?" then you run into the false cognate problem (a tree has bark while a dog can bark; a student can bomb a test while a fighter jet can bomb a test target).

    What you need is a way of asking "What named collec…

  • Aristotle commented on Are roles only semantically different from inheritance?

    Avoid complex inheritance hierarchies. This is equivalent to only allowing one level of inheritance.

    Yes, but then in order to do non-trivial specialisation you have to start inheriting from multiple things, like you would compose multiple roles. And then you run into problems because multiple superclasses can provide the same method – which one do you call? Do you call both? Do the two methods on those two superclasses mean the same thing?

    But with roles you do not have this problem. Why? Because roles become part of the class that consumes them, a…

  • jnareb commented on Are roles only semantically different from inheritance?

    Inheritance is (or should be) about is-a relationships fulfilling Liskov's substitution principle.

    One example that I have seen about roles is GuardDog class example: in Dog class hierarchy, and consuming Guard role (which can be consumed by many different unrelated classes).

Subscribe to feed Responses to Comments from mishin

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl and offering the modern features you’ve come to expect in blog platforms, the site is run by Dave Cross and Aaron Crane, with a design donated by Six Apart, Ltd.