假设我有一个专门的类和一个相关的伴侣对象:
trait Slice[@specialized +T] {
...
override def equals(that :Any) = that match {
case s :Slice[_] => ???
case _ => false
}
}
object Slice {
def newInstance[@specialized T] = ???
}
有没有办法检查
>如果此实例是专用子类,则在Slice方法内部,
>如果另一个实例是同一个基元的专用子类,则在Slice的方法内部,
>如果我正在运行已擦除或专用的变体,请在伴随对象上使用专门的方法
不依靠ClassTags或手动传递Class [_]?似乎信息应该是可用的,但我能想到的唯一方法是检查类的名称.
用例2)特别重要,因为我可以使用更快的算法,如果我是新的我将苹果与苹果进行比较.它可能可以通过反射来完成,但是当你考虑到我们必须处理Slice的非合成子类时,它会非常棘手;如果我们也有
trait ArraySlice[@specialized T] extends Slice[T] { ... }
应该被认为与Slice [T]实例“兼容”,只要它们都是专用的(或两者都被删除)?
最佳答案 好的,我想出了一个更清洁的方式:
final val AllButUnit = new Specializable.Group((Byte, Short, Int, Long, Char, Float, Double, Boolean, AnyRef))
def specializationFor[@specialized(AllButUnit) E] :ResolvedSpecialization[E] =
Specializations(new SpecializedKey[E]).asInstanceOf[ResolvedSpecialization[E]]
private val Specializations = Seq(
resolve[Byte],
resolve[Short],
resolve[Int],
resolve[Long],
resolve[Char],
resolve[Float],
resolve[Double],
resolve[Boolean],
resolve[Unit],
resolve[AnyRef]
).map(
spec => spec.key -> spec :(SpecializedKey[_], ResolvedSpecialization[_])
).toMap.withDefaultValue(resolve[AnyRef])
private def resolve[@specialized(AllButUnit) E :ClassTag] :ResolvedSpecialization[E] =
new ResolvedSpecialization[E](new SpecializedKey[E], new Array[E](0))
class ResolvedSpecialization[@specialized(AllButUnit) E] private[SpecializedCompanion]
(val array :Array[E], val elementType :Class[E], val classTag :ClassTag[E], private[SpecializedCompanion] val key :SpecializedKey[E])
{
private[SpecializedCompanion] def this(key :SpecializedKey[E], array :Array[E]) =
this(array, array.getClass.getComponentType.asInstanceOf[Class[E]], ClassTag(array.getClass.getComponentType.asInstanceOf[Class[E]]), key)
override def toString = s"@specialized($elementType)"
override def equals(that :Any) = that match {
case r :ResolvedSpecialization[_] => r.elementType==elementType
case _ => false
}
override def hashCode = elementType.hashCode
}
private class SpecializedKey[@specialized(AllButUnit) E] {
override def equals(that :Any) = that.getClass==getClass
override def hashCode = getClass.hashCode
def className = getClass.getName
override def toString = className.substring(className.indexOf("$")+1)
}
现在specializationFor [E] .elementType返回与E的特化参数对应的类.