From 0b3b12fb29b8a4a624c4a0bb4520114f10599036 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sat, 14 Apr 2012 11:40:00 +0100 Subject: Move primitive widening implicits to companions. Take a 15-implicit load off Predef and put it where it belongs: on those brave souls who like their longs to get floaty by way of T. --- src/compiler/scala/tools/cmd/gen/AnyVals.scala | 16 ++++++++- src/library/scala/Byte.scala | 5 +++ src/library/scala/Char.scala | 4 +++ src/library/scala/Float.scala | 1 + src/library/scala/Int.scala | 3 ++ src/library/scala/Long.scala | 2 ++ src/library/scala/Predef.scala | 48 +++++++++++++------------- src/library/scala/Short.scala | 4 +++ 8 files changed, 58 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/cmd/gen/AnyVals.scala b/src/compiler/scala/tools/cmd/gen/AnyVals.scala index 0869350dd3..b9f1ee2317 100644 --- a/src/compiler/scala/tools/cmd/gen/AnyVals.scala +++ b/src/compiler/scala/tools/cmd/gen/AnyVals.scala @@ -14,6 +14,20 @@ trait AnyValReps { sealed abstract class AnyValNum(name: String, repr: Option[String], javaEquiv: String) extends AnyValRep(name,repr,javaEquiv) { case class Op(val op : String, val doc : String) + + private def companionCoercions(tos: String*) = { + tos.toList map (to => + """implicit def %s2%s(x: %s): %s = x.to%s""".format(javaEquiv, to, name, to.capitalize, to.capitalize) + ) + } + def implicitCoercions: List[String] = javaEquiv match { + case "byte" => companionCoercions("short", "int", "long", "float", "double") + case "short" | "char" => companionCoercions("int", "long", "float", "double") + case "int" => companionCoercions("long", "float", "double") + case "long" => companionCoercions("float", "double") + case "float" => companionCoercions("double") + case _ => Nil + } def isCardinal: Boolean = isIntegerType(this) def unaryOps = { @@ -160,7 +174,7 @@ trait AnyValReps { } def objectLines = { val comp = if (isCardinal) cardinalCompanion else floatingCompanion - (comp + allCompanions).trim.lines map interpolate toList + ((comp + allCompanions).trim.lines map interpolate).toList ++ implicitCoercions } /** Makes a set of binary operations based on the given set of ops, args, and resultFn. diff --git a/src/library/scala/Byte.scala b/src/library/scala/Byte.scala index f9c5f6003e..4e64b68bbc 100644 --- a/src/library/scala/Byte.scala +++ b/src/library/scala/Byte.scala @@ -622,5 +622,10 @@ object Byte extends AnyValCompanion { /** The String representation of the scala.Byte companion object. */ override def toString = "object scala.Byte" + implicit def byte2short(x: Byte): Short = x.toShort + implicit def byte2int(x: Byte): Int = x.toInt + implicit def byte2long(x: Byte): Long = x.toLong + implicit def byte2float(x: Byte): Float = x.toFloat + implicit def byte2double(x: Byte): Double = x.toDouble } diff --git a/src/library/scala/Char.scala b/src/library/scala/Char.scala index 3d459782cd..8f770316c1 100644 --- a/src/library/scala/Char.scala +++ b/src/library/scala/Char.scala @@ -622,5 +622,9 @@ object Char extends AnyValCompanion { /** The String representation of the scala.Char companion object. */ override def toString = "object scala.Char" + implicit def char2int(x: Char): Int = x.toInt + implicit def char2long(x: Char): Long = x.toLong + implicit def char2float(x: Char): Float = x.toFloat + implicit def char2double(x: Char): Double = x.toDouble } diff --git a/src/library/scala/Float.scala b/src/library/scala/Float.scala index ff5b3cb112..ddbbd48bf4 100644 --- a/src/library/scala/Float.scala +++ b/src/library/scala/Float.scala @@ -401,5 +401,6 @@ object Float extends AnyValCompanion { /** The String representation of the scala.Float companion object. */ override def toString = "object scala.Float" + implicit def float2double(x: Float): Double = x.toDouble } diff --git a/src/library/scala/Int.scala b/src/library/scala/Int.scala index 316bbced2d..ef82b1f979 100644 --- a/src/library/scala/Int.scala +++ b/src/library/scala/Int.scala @@ -622,5 +622,8 @@ object Int extends AnyValCompanion { /** The String representation of the scala.Int companion object. */ override def toString = "object scala.Int" + implicit def int2long(x: Int): Long = x.toLong + implicit def int2float(x: Int): Float = x.toFloat + implicit def int2double(x: Int): Double = x.toDouble } diff --git a/src/library/scala/Long.scala b/src/library/scala/Long.scala index ce8618c22a..d57fe8b379 100644 --- a/src/library/scala/Long.scala +++ b/src/library/scala/Long.scala @@ -622,5 +622,7 @@ object Long extends AnyValCompanion { /** The String representation of the scala.Long companion object. */ override def toString = "object scala.Long" + implicit def long2float(x: Long): Float = x.toFloat + implicit def long2double(x: Long): Double = x.toDouble } diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala index 0954454e41..15e007528b 100644 --- a/src/library/scala/Predef.scala +++ b/src/library/scala/Predef.scala @@ -349,30 +349,30 @@ object Predef extends LowPriorityImplicits { // Primitive Widenings -------------------------------------------------------------- - implicit def byte2short(x: Byte): Short = x.toShort - implicit def byte2int(x: Byte): Int = x.toInt - implicit def byte2long(x: Byte): Long = x.toLong - implicit def byte2float(x: Byte): Float = x.toFloat - implicit def byte2double(x: Byte): Double = x.toDouble - - implicit def short2int(x: Short): Int = x.toInt - implicit def short2long(x: Short): Long = x.toLong - implicit def short2float(x: Short): Float = x.toFloat - implicit def short2double(x: Short): Double = x.toDouble - - implicit def char2int(x: Char): Int = x.toInt - implicit def char2long(x: Char): Long = x.toLong - implicit def char2float(x: Char): Float = x.toFloat - implicit def char2double(x: Char): Double = x.toDouble - - implicit def int2long(x: Int): Long = x.toLong - implicit def int2float(x: Int): Float = x.toFloat - implicit def int2double(x: Int): Double = x.toDouble - - implicit def long2float(x: Long): Float = x.toFloat - implicit def long2double(x: Long): Double = x.toDouble - - implicit def float2double(x: Float): Double = x.toDouble + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def byte2short(x: Byte): Short = x.toShort + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def byte2int(x: Byte): Int = x.toInt + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def byte2long(x: Byte): Long = x.toLong + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def byte2float(x: Byte): Float = x.toFloat + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def byte2double(x: Byte): Double = x.toDouble + + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def short2int(x: Short): Int = x.toInt + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def short2long(x: Short): Long = x.toLong + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def short2float(x: Short): Float = x.toFloat + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def short2double(x: Short): Double = x.toDouble + + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def char2int(x: Char): Int = x.toInt + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def char2long(x: Char): Long = x.toLong + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def char2float(x: Char): Float = x.toFloat + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def char2double(x: Char): Double = x.toDouble + + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def int2long(x: Int): Long = x.toLong + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def int2float(x: Int): Float = x.toFloat + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def int2double(x: Int): Double = x.toDouble + + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def long2float(x: Long): Float = x.toFloat + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def long2double(x: Long): Double = x.toDouble + + @deprecated("Use a method in an AnyVal's companion object", "2.10.0") def float2double(x: Float): Double = x.toDouble // "Autoboxing" and "Autounboxing" --------------------------------------------------- diff --git a/src/library/scala/Short.scala b/src/library/scala/Short.scala index 5664c3b44c..707f9bc4eb 100644 --- a/src/library/scala/Short.scala +++ b/src/library/scala/Short.scala @@ -622,5 +622,9 @@ object Short extends AnyValCompanion { /** The String representation of the scala.Short companion object. */ override def toString = "object scala.Short" + implicit def short2int(x: Short): Int = x.toInt + implicit def short2long(x: Short): Long = x.toLong + implicit def short2float(x: Short): Float = x.toFloat + implicit def short2double(x: Short): Double = x.toDouble } -- cgit v1.2.3