- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
r17712 - mesmeric & chimeric generic numeric
Tue, 2009-05-12, 15:38
I just committed a pretty good sized patch. It may take some minor
liberties, so if anyone objects to any part of it let me know and I'm
sure I can modify it to everyone's satisfaction. It involves the
implementations of Numeric, Ordering, and Range, and adds a generic
range type which can range among arbitrary types so long as they have an
Ordering and a Numeric.
https://lampsvn.epfl.ch/trac/scala/changeset/17712
The main achievement as far as I'm concerned was making it possible to
write the generic range almost exactly like the int-specific range, with
infix/punctuation operators on Ts. If anyone has ideas on implementing
any of it even more nicely, I'm all ears.
Mon, 2009-05-18, 10:37
#2
Re: r17712 - mesmeric & chimeric generic numeric
Of course I spoke slightly too soon. It turns out that as it currently
stands, this means there is no longer a unique implicit from Unit to
AnyRef. What sort of person would even want that, you might wonder? I
don't know, but before:
scala> val x: AnyRef = ()
x: AnyRef = ()
scala> val x: AnyRef = ()
:4: error: type mismatch;
found : Unit
required: AnyRef
Note that implicit conversions are not applicable because they are ambiguous:
both method orderingToOrdered in object Predef of type [T](T)(implicit Ordering[T])Ordered[T]
and method any2stringadd in object Predef of type (Any)scala.runtime.StringAdd
are possible conversion functions from Unit to AnyRef
val x: AnyRef = ()
^
I can either ditch the implementation of Ordering[Unit], or consider
this a feature a la the result of 1.getClass, or abandon the whole thing
because smooth implicits from Unit to AnyRef are the cornerstone of
Scala 3.0, or...
Hi Paul,
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] = ...
That way, we make a clean cut. From now on, everybidy should define
Orderings instead of Ordereds.
What do you think?
Cheers