R
Robert Dober
Hi list
I just released an implementation of traits tailor made for Ruby1.9.
http://rubyforge.org/projects/ruby-traits/
You can still use this library with Ruby1.8 but you cannot pass blocks
to behavior which
pretty much restricts its usefulness.
* This implements traits in the strict sense of their definition
here: http://portal.acm.org/ft_gateway.cf...=GUIDE&dl=GUIDE&CFID=7912496&CFTOKEN=77115102
They obey the flattening property, using a trait is exactly as defining
its methods inline, no modules are used and inheritance is unchanged.
All trait combination rules are obeyed (associative, communative, e.g.)
The main purpose of pure-traits is to play around with them and to see the
benefits and shortcomings of traits based design.
The syntax has changed, again inspired by Factes, trait aliasing is done
via #* now, thus traits aliased and combined can be expressed more
naturally:
t1 * { :a => :t1_a } + t2 * { :a => :t2_a }
works now as expected.
In addition to the traditional trait operations +, - and *, I have
implemented & to allow of selective usage of behavior.
I believe that the usage of this is normally a bad sign for the design
but sometimes it might be a handy feature.
Behavior in a trait is defiend with the message method accespting a
name and a block.
Only when a trait is used by a class or object the behavior is defined
as a method.
Modules do not play any role in this version.
The following examples shall just give a quick look and feel, please
refer to test/test-examples.rb
which is somehow written in tutotial style.
t = trait {
message :a do @a=42 end
message :b do |x,&blk| blk.call( x * @a ) end
}
class C
use t
end
c=C.new.a
c.b(42) do |x| puts x end ---> 1764
class C
use t, trait { message :a do 42 end }
end
C.new.a ---> Trait::TraitsConflict
u = Trait::new do
message :c do a+b(1){|x| x} end
end
(t + u).to_object.c --> 84
Enjoy
Robert
I just released an implementation of traits tailor made for Ruby1.9.
http://rubyforge.org/projects/ruby-traits/
You can still use this library with Ruby1.8 but you cannot pass blocks
to behavior which
pretty much restricts its usefulness.
* This implements traits in the strict sense of their definition
here: http://portal.acm.org/ft_gateway.cf...=GUIDE&dl=GUIDE&CFID=7912496&CFTOKEN=77115102
They obey the flattening property, using a trait is exactly as defining
its methods inline, no modules are used and inheritance is unchanged.
All trait combination rules are obeyed (associative, communative, e.g.)
The main purpose of pure-traits is to play around with them and to see the
benefits and shortcomings of traits based design.
The syntax has changed, again inspired by Factes, trait aliasing is done
via #* now, thus traits aliased and combined can be expressed more
naturally:
t1 * { :a => :t1_a } + t2 * { :a => :t2_a }
works now as expected.
In addition to the traditional trait operations +, - and *, I have
implemented & to allow of selective usage of behavior.
I believe that the usage of this is normally a bad sign for the design
but sometimes it might be a handy feature.
Behavior in a trait is defiend with the message method accespting a
name and a block.
Only when a trait is used by a class or object the behavior is defined
as a method.
Modules do not play any role in this version.
The following examples shall just give a quick look and feel, please
refer to test/test-examples.rb
which is somehow written in tutotial style.
t = trait {
message :a do @a=42 end
message :b do |x,&blk| blk.call( x * @a ) end
}
class C
use t
end
c=C.new.a
c.b(42) do |x| puts x end ---> 1764
class C
use t, trait { message :a do 42 end }
end
C.new.a ---> Trait::TraitsConflict
u = Trait::new do
message :c do a+b(1){|x| x} end
end
(t + u).to_object.c --> 84
Enjoy
Robert