为什么我不能在 scala 中专门化一个通用函数?
Why can't I specialize a generic function in scala?
对于具有以下签名的方法,我收到一条错误消息 type N is unused or used in non-specializable positions.
:
protected def offsetFrom0[@specialized(Int,Long) N](offsetFrom1 : Codec[N])(implicit N : Integral[N]) : Codec[N]
谁能用通俗易懂的语言向我解释一下专业化的规则是什么?
@specialized
注释可用于 class 和方法类型参数。
def gethead[@specialized(Int,Float,Double) T: Numeric](items: T*): T = items(0)
gethead(4,57,32) // Result: 4
所以在你的情况下,你可以按照以下方式做一些事情:
case class Offset[@specialized(Int, Long) N](offsetFrom1: N) {
def offsetFrom0: N = ???
}
Offset(1).offsetFrom0
Offset(1L).offsetFrom0
对于具有以下签名的方法,我收到一条错误消息 type N is unused or used in non-specializable positions.
:
protected def offsetFrom0[@specialized(Int,Long) N](offsetFrom1 : Codec[N])(implicit N : Integral[N]) : Codec[N]
谁能用通俗易懂的语言向我解释一下专业化的规则是什么?
@specialized
注释可用于 class 和方法类型参数。
def gethead[@specialized(Int,Float,Double) T: Numeric](items: T*): T = items(0)
gethead(4,57,32) // Result: 4
所以在你的情况下,你可以按照以下方式做一些事情:
case class Offset[@specialized(Int, Long) N](offsetFrom1: N) {
def offsetFrom0: N = ???
}
Offset(1).offsetFrom0
Offset(1L).offsetFrom0