diff options
author | Paul Phillips <paulp@improving.org> | 2012-09-16 20:44:56 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-09-16 20:53:59 -0700 |
commit | d918144f16111358a61d6f5f847227946bcc17a9 (patch) | |
tree | 55dd3ae2790e5ed453575205f83a700b6732dcd4 /src | |
parent | 5cf6a750eac94d159fe4c67b75e24dde13495e71 (diff) | |
download | scala-d918144f16111358a61d6f5f847227946bcc17a9.tar.gz scala-d918144f16111358a61d6f5f847227946bcc17a9.tar.bz2 scala-d918144f16111358a61d6f5f847227946bcc17a9.zip |
Moved constant empty arrays into Array companion.
This reminds me of at least one reason I didn't put them
here before: I don't feel like I can return these from calls
to Array.empty, because there's no way of knowing whether
anyone has been relying upon this property:
scala> Array.empty[Byte] eq Array.empty[Byte]
res0: Boolean = false
Since that is exactly the property I need to alter.
The test above is true in all the "real" collections,
which is all the more reason to be concerned that someone
might be using empty arrays as not-equivalent sentinels.
I can still move them here, but it'd be a lot better if
array creation could use them - not only def empty but
def apply[T: ClassTag](xs: T*): Array[T]
which probably creates millions of empty arrays where
one would do in almost every case.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/Array.scala | 10 | ||||
-rw-r--r-- | src/reflect/scala/tools/nsc/io/VirtualFile.scala | 2 | ||||
-rw-r--r-- | src/reflect/scala/tools/nsc/io/ZipArchive.scala | 2 |
3 files changed, 12 insertions, 2 deletions
diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala index c61a255e3b..0b8550be37 100644 --- a/src/library/scala/Array.scala +++ b/src/library/scala/Array.scala @@ -48,6 +48,16 @@ class FallbackArrayBuilding { * @version 1.0 */ object Array extends FallbackArrayBuilding { + val emptyBooleanArray = new Array[Boolean](0) + val emptyByteArray = new Array[Byte](0) + val emptyCharArray = new Array[Char](0) + val emptyDoubleArray = new Array[Double](0) + val emptyFloatArray = new Array[Float](0) + val emptyIntArray = new Array[Int](0) + val emptyLongArray = new Array[Long](0) + val emptyShortArray = new Array[Short](0) + val emptyObjectArray = new Array[Object](0) + implicit def canBuildFrom[T](implicit t: ClassTag[T]): CanBuildFrom[Array[_], T, Array[T]] = new CanBuildFrom[Array[_], T, Array[T]] { def apply(from: Array[_]) = ArrayBuilder.make[T]()(t) diff --git a/src/reflect/scala/tools/nsc/io/VirtualFile.scala b/src/reflect/scala/tools/nsc/io/VirtualFile.scala index 9061534edc..8a5114bfe7 100644 --- a/src/reflect/scala/tools/nsc/io/VirtualFile.scala +++ b/src/reflect/scala/tools/nsc/io/VirtualFile.scala @@ -33,7 +33,7 @@ class VirtualFile(val name: String, override val path: String) extends AbstractF //######################################################################## // Private data - private var content = Byte.emptyArray + private var content = Array.emptyByteArray //######################################################################## // Public Methods diff --git a/src/reflect/scala/tools/nsc/io/ZipArchive.scala b/src/reflect/scala/tools/nsc/io/ZipArchive.scala index 9d9d9a46f2..49d2200895 100644 --- a/src/reflect/scala/tools/nsc/io/ZipArchive.scala +++ b/src/reflect/scala/tools/nsc/io/ZipArchive.scala @@ -177,7 +177,7 @@ final class URLZipArchive(val url: URL) extends ZipArchive(null) { class FileEntry() extends Entry(zipEntry.getName) { override val toByteArray: Array[Byte] = { val len = zipEntry.getSize().toInt - val arr = if (len == 0) Byte.emptyArray else new Array[Byte](len) + val arr = if (len == 0) Array.emptyByteArray else new Array[Byte](len) var offset = 0 def loop() { |