Thursday, February 22, 2007

Perl 6 Roles ~= Squeak Traits?

Perl isn't very popular any more, but if Perl 6 ever actually happens, there could be some pleasant surprises involved. Perl is going to adopt elements of the Traits idea from Squeak Smalltalk. This is actually a pretty idiosyncratic decision -- to quote Larry Wall:

We buy the argument of the Traits paper that classes should not be used both to manage objects and to manage code reuse. It needs to be possible to separate those concerns. Since a lot of the code that people want to reuse is that which manages non-isa object relationships, that's what we should abstract out from classes.

That abstraction we are calling a role. Roles can encompass both interface and implementation of object relationships. A role without implementation degenerates to an interface. A role without interface degenerates to privately instantiated generics. But the typical role will provide both interface and at least a default implementation.

Unlike the Traits paper, we will allow state as part of our implementation.

The class approach to object-orientation is so widespread that many people don't even realize it's an approach to object-orientation. To most of the world, the idea of classes is what object-orientation means. I haven't played with Traits in Squeak yet, but as I understand it Squeak adds traits to the existing class system. Perl 6 is apparently planning to replace classes with traits, or rather, with something similar to traits, called roles. This could make the Perl 6 object system as weird, interesting, and nifty as the Common Lisp Object System (which is very, very odd).

I've grown very skeptical of Perl 6, any time a project takes forever and is supposed to be the best thing in the universe I figure it's never actually going to happen at all, but if the Perl 6 implementers prove me wrong, I think it'll be worth it to brush up on my Perl again. That would be cool.


  1. There is work being done on stateful traits. The original traits model does not use state for simplicity reasons (better have stateless traits working than stateful traits not working).

  2. Ah, so then the Squeak model may actually more closely match the Perl model soon.

    (God, that's a weird sentence.)

  3. It should read
    "Perl 6 Roles ~~ Squeak Traits?"
    because Roles and Regexes are matched with ~~ in Perl 6 ;-)

  4. Squeak has traits without state right now, which is effectively what Haskell has with type classes.

    But what is the point of stateful traits? How is that different then multiple inheritance (which has been tried and imo was not successful)?

    Oh, and Perl 6 is nothing to get excited about. You can have everything they offer "some time in the distant future" right now. And without ugly $%{->} syntax.


Note: Only a member of this blog may post a comment.