develooper Front page | perl.perl6.language | Postings from December 2003

Re: Roles and Mix-ins?

Thread Previous | Thread Next
Luke Palmer
December 13, 2003 03:57
Re: Roles and Mix-ins?
Message ID:
Chris Shawmail (E-mail) writes:
> I'm still digesting the vocabulary thread, but while I do, let me ask a
> question that's probably crystal clear to everyone else.
> Do roles act as a form of mix-in, as Ruby modules may, and Objective-C
> protocols do?
> Would the following two snippets be at all equivalent?

Probably, depending on what's in the eventual definition of Foo.

Roles are quite similar to mixins (as the Traits paper said that they
were inspired by mixins), but they're distinctly not the same. 

For one, one role's methods don't silently override another's.  Instead,
you get, er, role conflict and you have to disambiguate yourself.  For
two, you can attach new roles to an object at runtime (I don't know if
you can do this with mixins, actually).

> # Perl6
> role Talk {
>    method say_hello {
>       print "Hello world!\n"
>    }
> }
> class Foo does Talk { ... }
> # Ruby
> module Talk
>    def say_hello
>       puts "Hello world!"
>    end
> end
> class Foo
>    include Talk
> end


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About