Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么可以';我不能在scala中专门化泛型函数吗?_Scala_Template Specialization_Specialized Annotation - Fatal编程技术网

为什么可以';我不能在scala中专门化泛型函数吗?

为什么可以';我不能在scala中专门化泛型函数吗?,scala,template-specialization,specialized-annotation,Scala,Template Specialization,Specialized Annotation,我收到一个错误,说类型N未使用或用于非专用位置。,对于具有以下签名的方法: protected def offsetFrom0[@specialized(Int,Long) N](offsetFrom1 : Codec[N])(implicit N : Integral[N]) : Codec[N] 有人能用外行的术语向我解释一下专门化的规则吗?这个@specialized注释可以用于类和方法类型参数 def gethead[@specialized(Int,Float,Double) T:

我收到一个错误,说
类型N未使用或用于非专用位置。
,对于具有以下签名的方法:

protected def offsetFrom0[@specialized(Int,Long) N](offsetFrom1 : Codec[N])(implicit N : Integral[N]) : Codec[N]

有人能用外行的术语向我解释一下专门化的规则吗?

这个
@specialized
注释可以用于类和方法类型参数

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

@specialized
注释可用于类和方法类型参数

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