From: Mathieu Bouchard Date: 2000-09-22T14:27:16+09:00 Subject: [ruby-talk:5056] Re: Perl 6 rumblings -- RFC 225 (v1) Data: S uperpositions (fwd) > While I can't grasp a bit what's going on with superpositions, I might add > that I'd bet there's never need for this type of RFC ([1]) for Ruby. I'm not > talking about the subject though; we might want to implement superpositions > in Ruby too. But in this case one has more freedom in Ruby than in Perl. ok, let's see what's going on. a = Any.new(1,3,6,10) # a is a superposition of four integers. b = a > 5 # b is a superposition of four booleans, the results of >(5) messages to all elements. b.collapse true the final result is a OR of all booleans. the source code: --------8<--------cut-here--------8<-------- class Any def initialize(*values) @values = values end def method_missing(*params,&proc) Any.new(@values.collect {|v| v.send(*params,&proc) }) end def collapse @values.each {|v| return true if v } return false end end --------8<--------cut-here--------8<-------- Of course the above code is not perfect. In particular it does not deal with the fact that several built-in methods check for particular types, most notably Numeric#coerce; and also the evaluator does check for nil/false, so you can't automatically collapse on if..then, because if..then sees any Any object as a true value. Maybe this could be solved (partially) by having an Object#to_b that is required to return true or false (the real ones). Maybe also an if..then should be an Object#then_else method which is passed two blocks (which has the advantage of allowing us to change the semantics of if/then/else completely if needed). My point is, although it might not be wanted to add superpositions directly in the language, some peripheral, general-purpose features might be desirable. matju