As sort of an academic exercise, I've been thinking about how one could re-envision Perl's object model using closures as objects, without using the bless() function (because otherwise, of course, that's not really re-envisioning Perl's object model — it's just modifying it slightly).

Well, one problem is that bless() and packages are the way in which Perl does it's method dispatching. Without hacking the core of Perl and making the method dispatching more "hookable", you will find that you will end up jumping through many syntactic hoops to make things work (or using AUTOLOAD which pretty much rules it out as a tool for "serious" use).

As a quick aside, you might want to take a look at something I did while working on the Perl 6 metamodel for Pugs, it was a small prototype metamodel based on one written in Scheme. It is basically closure based objects like you suggest, with a slight layer of "sugar" to make them a little more Perl 5 friendly.

Now, I will skip the rest of your post and go right to the updated question.

What are the fundamental problems inheritance is meant to solve, and how can we solve them in relation to a closure-based object model for Perl 5.x?

My first suggestion is to read read read. There are many people thinking about such things, and much time, effort and research has been put into trying to answer this question and then solve it. Here is a quick list of some good resources:

Now, onto your question.

Inheritance is not really a bad thing when used appropriately. Some things are really best described with an single inheritence is-a relationship. However, other things are not so simply strutured. But once multiple inheritence comes into the picture, inheritence becomes a really nasty thing. This is where mixins, traits and roles come in with their "sets of behaviors" which can be "injected" into a class (I tend to think of inheritence as being a vertical thing, while (mixin|trait|role)s are a horizontal thing).

From a not-really-OO-but-kinda-like-OO perspective, there is the Standard ML module system and Haskell's TypeClasses. Here is a link to a fairly well received paper comparing the two, but I have to admit I have not read it yet so I cannot vouch for it. Both these systems take a very "first-class" view of modular units, and create very powerful (and sometimes mind-bendingly complex) systems with it.

I see all of these things as attempting to solve the basic problem of modular composition, or to phrase it as a question; "How to compose behaviors and state together in a highly reusable fashion?". I think inheritence is simply one part of the solution (albeit a very overused/abused one), and should not be tossed out because it really does have it's place. If properly used, along with other tools such as (roles|traits), mixins and generics, it is a good thing.

Anyway, lunch break is over, time to get back to $work. Enjoy the links.

-stvn

In reply to Re: Inheritance: the root of the problem by stvn
in thread Inheritance: the root of the problem by apotheon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.