From: Florian Gross Date: 2003-10-02T04:00:15+09:00 Subject: Re: Making == symmetric? Moin! Nathan Weston wrote: >>> trying to implement perl6-style junctions > Suppose we have a method any(*args), which creates a junction object > of its arguments. > > Then any("1", "2", "3") == "1" evaluates to true (which is correct). > However, "1" == any("1", "2", "3") evaluates to false (which is not > correct). > > The problem is that String doesn't know about the Junction class, and > therefore assumes it is not equal to any instances of it. (Maybe it > looks for a to_str method first, or somesuch -- I don't know the > implementation details exactly). There is no way around this, no > matter how you implement Junction, because in the second case the > comparison is don exclusively by String -- none of Junction's methods > are ever called. Junctions are interesting things to implement. (Even if it's questionable whether they really make sense in Ruby when doing something else than passing multiple objects to a method which only expects a single one...) I've actually implemented them already in a few lines of plain Ruby code: http://rubyforge.org/projects/junction/ I solved the problem of built-in methods not knowing what to do with Junctions by plainly hiding them behind proxies -- this makes interesting things like "food taste good".match(%w{foo bar qux}.any) possible. The problem with this approach is that it's quite ugly to implement and that it might even cause some bad side-effects. (Because of this I didn't include this in the actual Junction implementation itself.) Oh, and to really make Junctions perl compatible you would need to replace any method call including Junctions as parameters with a Junction constructed of the results of the method call with all the Junction's states anyway. (Perl6 doesn't pass Junctions into methods by default because it fears that Junction logic might be confusing. (For example in this code you could get both "foo!" and "bar!" if arg is a Junction: puts "foo!" if arg == 1; puts "bar!" if arg == 2)) Regards, Florian Gro�