This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: r17712 - mesmeric & chimeric generic numeric

1 reply
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.

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.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: r17712 - mesmeric & chimeric generic numeric

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.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland