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

the type with more wrapper classes than legal values

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

I find this more amusing than is healthy:

While investigating some fascinating aspects of BoxedUnit I was reminded
of RichUnit, presented here in its entirety:

/** This class exists only as a dummy subclass so that there are two ambiguous
* implicit conversions from Unit to some subclass to Object.
* It's important that this class should NOT inherit from Ordered
*/
final class RichUnit {}

Fair enough, but something of a surprise to me because a while back I
ripped all the foo2ordered implicits out of Predef, replacing them all
with a single orderingToOrdered implicit -- all that is except for
unit2ordered, which I left in because there was a test case depending
(unintentionally I suspect) on a successful implicit conversion from
Unit to AnyRef. As my comment in Ordering still says:

// XXX For the time being this is non-implicit so there remains
// only one default implicit conversion Unit => AnyRef (the other
// being any2stringadd in Predef.)

So wait, if RichUnit only exists to foil () from ascending to
objecthood, how could a test case have been relying on that very moment
of rapture? Let's look at a few lines from Predef:

implicit def floatWrapper(x: Float) = new runtime.RichFloat(x)
implicit def doubleWrapper(x: Double) = new runtime.RichDouble(x)
implicit def booleanWrapper(x: Boolean) = new runtime.RichBoolean(x)
implicit def unitWrapper(x: Boolean) = new runtime.RichUnit
implicit def stringWrapper(x: String) = new runtime.RichString(x)

"One of these conversions is not like the others... one of these pimps
is doing his own thing..."

As a final jab at Cutty McPastington, the $Id line from RichUnit boldly
declares itself:

// $Id: RichInt.scala

Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: the type with more wrapper classes than legal values

On Sun, Jun 21, 2009 at 2:18 AM, Paul Phillips wrote:
> I find this more amusing than is healthy:
>
> While investigating some fascinating aspects of BoxedUnit I was reminded
> of RichUnit, presented here in its entirety:
>
>  /** This class exists only as a dummy subclass so that there are two ambiguous
>   *  implicit conversions from Unit to some subclass to Object.
>   *  It's important that this class should NOT inherit from Ordered
>   */
>  final class RichUnit {}
>
> Fair enough, but something of a surprise to me because a while back I
> ripped all the foo2ordered implicits out of Predef, replacing them all
> with a single orderingToOrdered implicit -- all that is except for
> unit2ordered, which I left in because there was a test case depending
> (unintentionally I suspect) on a successful implicit conversion from
> Unit to AnyRef.  As my comment in Ordering still says:
>
>  // XXX For the time being this is non-implicit so there remains
>  // only one default implicit conversion Unit => AnyRef (the other
>  // being any2stringadd in Predef.)
>
> So wait, if RichUnit only exists to foil () from ascending to
> objecthood, how could a test case have been relying on that very moment
> of rapture? Let's look at a few lines from Predef:
>
>  implicit def floatWrapper(x: Float)   = new runtime.RichFloat(x)
>  implicit def doubleWrapper(x: Double) = new runtime.RichDouble(x)
>  implicit def booleanWrapper(x: Boolean)  = new runtime.RichBoolean(x)
>  implicit def unitWrapper(x: Boolean)  = new runtime.RichUnit

I like such heart warming tales. But are you sure that Boolean
doesn't really want to be a RichUnit when it grows up?

I actually see this as one of the best things about Scala, not only
are the devs accessible and the code open but the devs too are open
about their little scala dragons/bugs in the code. Great stuff.

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