为什么ArrayBuilder base class 不提供方法接口?
Why does the ArrayBuilder base class not provide method interfaces?
ArrayBuilder 没有方法定义:
abstract class ArrayBuilder[T] extends ReusableBuilder[T, Array[T]] with Serializable
然而,它的实现通常会共享具有相同接口的方法,直到泛型类型参数 T
(以 mkArray
为例):
final class ofFloat extends ArrayBuilder[Float] {
//...
private def mkArray(size: Int): Array[Float] = {
val newelems = new Array[Float](size)
if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
newelems
}
在引入 ClassTag
之前,无法抽象出创建新数组的方法 "up to the generic type argument T
";之后他们可以,但它会损失性能(在大多数情况下可能非常轻微,但这段代码经常被调用......)。
类型擦除与数组的交互很奇怪。 ArrayBuilder[T]
中的任何 Array[T]
最终都会成为 Array[AnyRef]
。因此,如果您在那里只有抽象方法,类 就像 ofFloat
最终会产生很多隐藏的强制转换,JIT 可能会优化也可能不会优化。
ArrayBuilder 没有方法定义:
abstract class ArrayBuilder[T] extends ReusableBuilder[T, Array[T]] with Serializable
然而,它的实现通常会共享具有相同接口的方法,直到泛型类型参数 T
(以 mkArray
为例):
final class ofFloat extends ArrayBuilder[Float] {
//...
private def mkArray(size: Int): Array[Float] = {
val newelems = new Array[Float](size)
if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
newelems
}
在引入
ClassTag
之前,无法抽象出创建新数组的方法 "up to the generic type argumentT
";之后他们可以,但它会损失性能(在大多数情况下可能非常轻微,但这段代码经常被调用......)。类型擦除与数组的交互很奇怪。
ArrayBuilder[T]
中的任何Array[T]
最终都会成为Array[AnyRef]
。因此,如果您在那里只有抽象方法,类 就像ofFloat
最终会产生很多隐藏的强制转换,JIT 可能会优化也可能不会优化。