From 9d84e89d27c396203e84f6ae685863210ebc1968 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Fri, 14 Sep 2012 10:54:23 -0700 Subject: Added constant empty array to the companion objects. Because there are lots of times when you just need an array and shouldn't have to allocate one every time or pick a random spot to cache yet another empty array. --- src/compiler/scala/tools/cmd/gen/AnyVals.scala | 17 ++++++++++------- src/library/scala/Boolean.scala | 7 +++++++ src/library/scala/Byte.scala | 8 ++++++++ src/library/scala/Char.scala | 8 ++++++++ src/library/scala/Double.scala | 8 ++++++++ src/library/scala/Float.scala | 8 ++++++++ src/library/scala/Int.scala | 8 ++++++++ src/library/scala/Long.scala | 8 ++++++++ src/library/scala/Short.scala | 8 ++++++++ test/files/run/empty-array.check | 3 +++ test/files/run/empty-array.scala | 8 ++++++++ 11 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 test/files/run/empty-array.check create mode 100644 test/files/run/empty-array.scala diff --git a/src/compiler/scala/tools/cmd/gen/AnyVals.scala b/src/compiler/scala/tools/cmd/gen/AnyVals.scala index 7842603af7..3f8283590c 100644 --- a/src/compiler/scala/tools/cmd/gen/AnyVals.scala +++ b/src/compiler/scala/tools/cmd/gen/AnyVals.scala @@ -14,7 +14,7 @@ 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: AnyValRep*) = { tos.toList map (to => """implicit def @javaequiv@2%s(x: @name@): %s = x.to%s""".format(to.javaEquiv, to.name, to.name) @@ -24,7 +24,7 @@ trait AnyValReps { def coercionComment = """ /** Language mandated coercions from @name@ to "wider" types.%s */""".format(coercionCommentExtra) - + def implicitCoercions: List[String] = { val coercions = this match { case B => companionCoercions(S, I, L, F, D) @@ -247,7 +247,7 @@ trait AnyValReps { def classDoc = interpolate(classDocTemplate) def objectDoc = "" def mkImports = "" - + def mkClass = assemble("final abstract class " + name + " private extends AnyVal", classLines) def mkObject = assemble("object " + name + " extends AnyValCompanion", objectLines) def make() = List[String]( @@ -320,7 +320,13 @@ def unbox(x: java.lang.Object): @name@ = @unboxImpl@ override def toString = "object scala.@name@" """ - def nonUnitCompanions = "" // todo + def nonUnitCompanions = """ +/** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[@name@] + */ +final val emptyArray = new Array[@name@](0)""" def cardinalCompanion = """ /** The smallest value representable as a @name@. @@ -341,9 +347,6 @@ final val NaN = @boxed@.NaN final val PositiveInfinity = @boxed@.POSITIVE_INFINITY final val NegativeInfinity = @boxed@.NEGATIVE_INFINITY -@deprecated("use @name@.MinPositiveValue instead", "2.9.0") -final val Epsilon = MinPositiveValue - /** The negative number with the greatest (finite) absolute value which is representable * by a @name@. Note that it differs from [[java.lang.@name@.MIN_VALUE]], which * is the smallest positive value representable by a @name@. In Scala that number diff --git a/src/library/scala/Boolean.scala b/src/library/scala/Boolean.scala index 014928d986..617c2eac36 100644 --- a/src/library/scala/Boolean.scala +++ b/src/library/scala/Boolean.scala @@ -135,5 +135,12 @@ object Boolean extends AnyValCompanion { */ override def toString = "object scala.Boolean" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Boolean] + */ + final val emptyArray = new Array[Boolean](0) } diff --git a/src/library/scala/Byte.scala b/src/library/scala/Byte.scala index 6f54f6cedf..7a5a36c5f6 100644 --- a/src/library/scala/Byte.scala +++ b/src/library/scala/Byte.scala @@ -625,6 +625,14 @@ object Byte extends AnyValCompanion { */ override def toString = "object scala.Byte" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Byte] + */ + final val emptyArray = new Array[Byte](0) + /** Language mandated coercions from Byte to "wider" types. */ implicit def byte2short(x: Byte): Short = x.toShort diff --git a/src/library/scala/Char.scala b/src/library/scala/Char.scala index b681ae1693..5b0caf327f 100644 --- a/src/library/scala/Char.scala +++ b/src/library/scala/Char.scala @@ -625,6 +625,14 @@ object Char extends AnyValCompanion { */ override def toString = "object scala.Char" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Char] + */ + final val emptyArray = new Array[Char](0) + /** Language mandated coercions from Char to "wider" types. */ implicit def char2int(x: Char): Int = x.toInt diff --git a/src/library/scala/Double.scala b/src/library/scala/Double.scala index 510de92a2a..09b393e515 100644 --- a/src/library/scala/Double.scala +++ b/src/library/scala/Double.scala @@ -400,5 +400,13 @@ object Double extends AnyValCompanion { /** The String representation of the scala.Double companion object. */ override def toString = "object scala.Double" + + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Double] + */ + final val emptyArray = new Array[Double](0) } diff --git a/src/library/scala/Float.scala b/src/library/scala/Float.scala index b9c116da0b..4aa74244b0 100644 --- a/src/library/scala/Float.scala +++ b/src/library/scala/Float.scala @@ -401,6 +401,14 @@ object Float extends AnyValCompanion { */ override def toString = "object scala.Float" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Float] + */ + final val emptyArray = new Array[Float](0) + /** Language mandated coercions from Float to "wider" types. */ implicit def float2double(x: Float): Double = x.toDouble diff --git a/src/library/scala/Int.scala b/src/library/scala/Int.scala index b2a4f93253..8f05596633 100644 --- a/src/library/scala/Int.scala +++ b/src/library/scala/Int.scala @@ -625,6 +625,14 @@ object Int extends AnyValCompanion { */ override def toString = "object scala.Int" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Int] + */ + final val emptyArray = new Array[Int](0) + /** Language mandated coercions from Int to "wider" types. */ implicit def int2long(x: Int): Long = x.toLong diff --git a/src/library/scala/Long.scala b/src/library/scala/Long.scala index 40932a65a7..7447fa3a15 100644 --- a/src/library/scala/Long.scala +++ b/src/library/scala/Long.scala @@ -625,6 +625,14 @@ object Long extends AnyValCompanion { */ override def toString = "object scala.Long" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Long] + */ + final val emptyArray = new Array[Long](0) + /** Language mandated coercions from Long to "wider" types. */ implicit def long2float(x: Long): Float = x.toFloat diff --git a/src/library/scala/Short.scala b/src/library/scala/Short.scala index 687b198a11..af7eb699d8 100644 --- a/src/library/scala/Short.scala +++ b/src/library/scala/Short.scala @@ -625,6 +625,14 @@ object Short extends AnyValCompanion { */ override def toString = "object scala.Short" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Short] + */ + final val emptyArray = new Array[Short](0) + /** Language mandated coercions from Short to "wider" types. */ implicit def short2int(x: Short): Int = x.toInt diff --git a/test/files/run/empty-array.check b/test/files/run/empty-array.check new file mode 100644 index 0000000000..bb0b1cf658 --- /dev/null +++ b/test/files/run/empty-array.check @@ -0,0 +1,3 @@ +0 +0 +0 diff --git a/test/files/run/empty-array.scala b/test/files/run/empty-array.scala new file mode 100644 index 0000000000..e56c86df5c --- /dev/null +++ b/test/files/run/empty-array.scala @@ -0,0 +1,8 @@ +object Test { + def main(args: Array[String]): Unit = { + println(Byte.emptyArray.length) + println(Double.emptyArray.length) + println(Boolean.emptyArray.length) + // okay okay okay + } +} -- cgit v1.2.3