Scala type error - takes no type parameters, expec

2019-05-08 06:42发布

问题:

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.

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]                                                                                                     ^  

回答1:

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]})#λ]() {