Scala类型错误-不接受任何类型参数,应为:1 [英] Scala type error - takes no type parameters, expected: one
问题描述
Scala的其他开发人员,
Hi fellow Scala developers,
任何人都可以在下面的代码中向我解释类型推断出什么问题以及如何解决该问题.
Could anyone please explain to me what is wrong with type inference in the following code and how it can be fixed.
以下代码是使用Scala 2.10.2的Play 2.2的自定义操作
The following code is a custom action for Play 2.2 using Scala 2.10.2
class Test {
trait Entity
class NodeRequest[A,K <:Entity](val entity: K,
val request: Request[A])
extends WrappedRequest[A](request)
def LocateResource[A,K](itemId: Int, v: List[K],
forceOwners:Boolean = true) =
new ActionBuilder[NodeRequest[A,K]]() {
def invokeBlock[A](request: Request[A],
block: (NodeRequest[A,K]) => Future[SimpleResult]) = {
Future.successful(Ok)
}
}
[error] Test.this.NodeRequest[A,K] takes no type parameters, expected: one
[error] def LocateResource[A,K](itemId: Int, v: List[K] , forceOwners:Boolean = true) = new ActionBuilder[NodeRequest[A,K]]() {
[error] ^
推荐答案
错误消息有点令人困惑-它实际上是指ActionBuilder的类型参数.您需要一个类型函数(或更具体地说,是一个部分类型应用程序).在Scala中,这有点棘手.实际上,Scala 2.8语言参考说您不能做到这一点,但这不再是真的.试试这个:
The error message is a bit confusing - it actually refers to the type parameter of ActionBuilder. What you need is a type function (or more particularly, a partial type application). This is a bit tricky in Scala. The Scala 2.8 language reference actually says you can't do it, but that is not true any more. Try this:
def LocateResource[A,K](itemId: Int, v: List[K],
forceOwners:Boolean = true) =
new ActionBuilder[({type λ[B] = NodeRequest[B,K]})#λ]() {
这篇关于Scala类型错误-不接受任何类型参数,应为:1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!