- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: r17712 - mesmeric & chimeric generic numeric
Thu, 2009-05-14, 21:51
On Thu, May 14, 2009 at 10:25:55PM +0200, martin odersky wrote:
> At first glance, this looks very good to me. We needed to flesh out
> the rudimentary Numeric and Ordering classes and this seems to do it
> just fine. One thing we also need to do is bridge Ordered and
> Ordering. I thought of eliminating all Ordered implicit conversions in
> the library and replacing them by a single one:
>
> implicit def orderingToOrdered[T](x: T)(implicit ord: Ordering[T]):
> Ordered[T] = ...
Consolidating overlapping features? Shrinking Predef? Did I get hit by a
bus and wake up in heaven?
I'm off to find out if reality has any objections.
On Thu, May 14, 2009 at 10:25:55PM +0200, martin odersky wrote:
> I thought of eliminating all Ordered implicit conversions in
> the library and replacing them by a single one:
>
> implicit def orderingToOrdered[T](x: T)(implicit ord: Ordering[T]):
> Ordered[T] = ...
It is with no small amount of surprise that I report that replacing all
those implicits with
implicit def orderingToOrdered[T](x: T)(implicit ord: Ordering[T]): Ordered[T] =
new Ordered[T] { def compare(that: T): Int = ord.compare(x, that) }
worked perfectly, all tests passing (modulo a few irrelevant changes to
error message on tests expected to fail.)
I had to make exactly one modification because scala is funny like that.
This method:
def compareLists[T <% Ordered[T]](xs: List[T], ys: List[T]) =
xs.sort(_ < _) == ys.sort(_ < _)
It made me stick ": Boolean" in there, just to show me it could, I think.