Fwd: [Squeak-fr] Ruby et les Traits.

Ramon Leon ramon.leon at allresnet.com
Mon Oct 22 21:44:34 UTC 2007


> -----Original Message-----
> From: squeak-dev-bounces at lists.squeakfoundation.org 
> [mailto:squeak-dev-bounces at lists.squeakfoundation.org] On 
> Behalf Of Andreas Raab
> Sent: Monday, October 22, 2007 2:04 PM
> To: The general-purpose Squeak developers list
> Subject: Re: Fwd: [Squeak-fr] Ruby et les Traits.
> 
> Ramon Leon wrote:
> > Traits can be composed better because you can specify which methods 
> > you want from conflicting traits explicitly.  Ruby's 
> modules I believe 
> > don't allow you to mix and match, but is based on inclusion 
> order only.
> 
> Do you have an example for this? I'm uncertain what a 
> "conflicting trait" is and how this would be resolved (in 
> either implementation).
> 
> Cheers,
>    - Andreas

Well, should two traits both have methods of the same name, Traits lets you
pick and choose which one you want..

Object subclass: #CustomHero
	uses: SupermanTrait - {#run. #fly. #think} + FlashTrait - {#heal.
#think} + BatmanTrait -{#heal. #run}
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'SomeCategory'

So you can manually mix and match Traits to get the desired implementation,
in this case a Hero that's like Superman but can't fly, is still
invulnerable, runs as fast as the Flash and is as smart as Batman.   Or you
want to compose a new Trait by mixing and matching methods from several
other traits.  

More realistically say you have an Enumerable Trait that only requires you
implement #do: in your class... You may not want to provide everything the
module provides, so you selectively remove #select:, #detect: while allowing
the rest.  

Ruby's modules don't allow this level of control.  Given modules with
conflicting methods, you either get all from one or all from the other
depending on the order you include them.  That is my understanding anyway.

Ramon Leon
http://onsmalltalk.com




More information about the Squeak-dev mailing list