From: Martin Duerst Date: 2007-12-24T16:29:02+09:00 Subject: Re: Why is Array not Comparable At 10:40 07/12/23, Rick DeNatale wrote: >On Dec 22, 2007 8:17 PM, Martin Duerst wrote: >> I just bumped into this: >> Why does Array not include Comparable? >> Array defines <=>, and so including Comparable should be easy. >> >> It looks to me like a big oversight, or there must have been >> some very deep reasons to not do this, which I'm completely ignorant of. >> > >Good question. > >You can of course either open Array and include comparable, or extend >an individual array instance. > >But some subtleties. Good points, but they only make good reasons for why Array might not even want to support <=>, or why users have to be careful about <=> (and therefore <, <=, >, >=, ==, !=, if availale), not for why <=> is available but not the rest. Regards, Martin. >1) Just because Array implements <=> doesn't mean that it will work as >you expect for all arrays. Array basically implements <=> by >comparing elements with the argument until either an element >comparison returns a non-zero or either the receiver or argument 'runs >out' of elements. > >So if elements aren't comparable with <=> pairwise ... > >2) Comparable not only requires that <=> be implemented but that it >return -1, 0, or 1. Some classes implement it to return nil if the >argument isn't of the right class, so even if Array included >comparable things like ["a] < [1] wouldn't work. #-#-# Martin J. Du"rst, Assoc. Professor, Aoyama Gakuin University #-#-# http://www.sw.it.aoyama.ac.jp mailto:duerst@it.aoyama.ac.jp