Hi list
I have just released ruby-traits v0.1. This are Traits in pure Ruby 1.8.
Have fun.
···
----------------------------------------------------------------------------------------
Traits for Ruby
Traits are Composable Units of Behavior, well that is the
academic title,
For Rubiests the following would be a good definition:
Mixins with Conflict Resolution and a Flattening Property
allowing to avoid subtle problems like Double Inclusion and
calling method x from Mixin L while we wanted method x from
Mixin N.
There is some (extra/nice?) composition syntax too
For details please refer to:
http://portal.acm.org/ft_gateway.cfm?id=1028771&type=pdf&coll=GUIDE&dl=GUIDE&CFID=7912496&CFTOKEN=77115102
which is a PhD thesis defining traits formally.
And yes Traits are implemented in Squeak 3.9.
In practice Traits enable us to:
* get a RuntimeError when we call a method defined by
more than one trait.
These conflicts can be resolved by redefining the method.
* avoid any double inclusion problem.
* compose Traits
* alias methods during Trait Composition
* resolve super dynamically (mentioned for completeness, Ruby modules
can do this too, of course
Examples:
t1 = trait { def a; 40 end }
t2 = Trait::new{ def a; 2 end }
c1 = Class::new {
use t1, t2
}
c1.new.a --> raises TraitsConflict
conflicts can be resolved be redefinition, and aliasing can be used for
access to the overriden methods. All this can be combined
with traits composition.
t = ( t1 + { :a => :t1_a } ) + ( t2 + {:a => :t2_a } )
c2 = Class::new {
use t
def a; t1_a + t2_a end
}
c2.new.a --> 42