diff options
Diffstat (limited to 'library/src/dotty/runtime/Arrays.scala')
-rw-r--r-- | library/src/dotty/runtime/Arrays.scala | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/library/src/dotty/runtime/Arrays.scala b/library/src/dotty/runtime/Arrays.scala new file mode 100644 index 000000000..9ec5512ad --- /dev/null +++ b/library/src/dotty/runtime/Arrays.scala @@ -0,0 +1,31 @@ +package dotty.runtime + +import scala.reflect.ClassTag + +import java.lang.{reflect => jlr} + +/** All but the first two operations should be short-circuited and implemented specially by + * the backend. + */ +object Arrays { + + // note: this class is magical. Do not touch it unless you know what you are doing.` + + /** Creates an array of some element type determined by the given `ClassTag` + * argument. The erased type of applications of this method is `Object`. + */ + def newGenericArray[T](length: Int)(implicit tag: ClassTag[T]): Array[T] = + tag.newArray(length) + + /** Convert a sequence to a Java array with element type given by `clazz`. */ + def seqToArray[T](xs: Seq[T], clazz: Class[_]): Array[T] = { + val arr = java.lang.reflect.Array.newInstance(clazz, xs.length).asInstanceOf[Array[T]] + xs.copyToArray(arr) + arr + } + + /** Create an array of a reference type T. + */ + def newArray[Arr](componentType: Class[_], returnType: Class[Arr], dimensions: Array[Int]): Arr = + jlr.Array.newInstance(componentType, dimensions: _*).asInstanceOf[Arr] +} |