关于编译器错误:Scala 用 () 应用 PartialFunction 与 .apply() 不一样

Scala applying a PartialFunction with () is not the same as .apply()

我想在一个项目(Play Framework 2.4)中重构我的 scala 代码时,我想到了这个想法:

(为了提供一个最小的工作示例,我已经更改了一些类,例如,我分别用 Int 和 Option[Int] 更改了 Result 和 Future[Result])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
object ParFuncApply {

  trait CanBeAuthenticatedRequest[A]
  trait AuthenticatedRequest[A] extends CanBeAuthenticatedRequest[A]
  trait UnauthenticatedRequest[A] extends CanBeAuthenticatedRequest[A]


  private def fold[T](authenticated: (AuthenticatedRequest[_]) => T)
                             (unauthenticated: (UnauthenticatedRequest[_]) => T):
  PartialFunction[CanBeAuthenticatedRequest[_], T] = {
    case ar: AuthenticatedRequest[_] => authenticated(ar)
    case ur: UnauthenticatedRequest[_] => unauthenticated(ur)
  }

  def apply(request: CanBeAuthenticatedRequest[_])
           (authenticated: (AuthenticatedRequest[_]) => Int)
           (unauthenticated: (UnauthenticatedRequest[_]) => Int): Int = {
    fold(authenticated)(unauthenticated)(request)
  }

  def async(request: CanBeAuthenticatedRequest[_])
           (authenticated: (AuthenticatedRequest[_]) => Option[Int])
           (unauthenticated: (UnauthenticatedRequest[_]) => Option[Int]): Option[Int] = {
    fold(authenticated)(unauthenticated)(request)
  }
}

上面的代码编译好了。

然后我:我应该将 fold[T] 参数化类型限制为 Int 和 Option[Int],所以我添加了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
object ParFuncApply {

  trait CanBeAuthenticatedRequest[A]
  trait AuthenticatedRequest[A] extends CanBeAuthenticatedRequest[A]
  trait UnauthenticatedRequest[A] extends CanBeAuthenticatedRequest[A]

  sealed trait Helper[T]

  object Helper {
    implicit object FutureResultHelper extends Helper[Option[Int]]
    implicit object ResultHelper extends Helper[Int]
  }

  private def fold[T: Helper](authenticated: (AuthenticatedRequest[_]) => T)
                             (unauthenticated: (UnauthenticatedRequest[_]) => T):
  PartialFunction[CanBeAuthenticatedRequest[_], T] = {
    case ar: AuthenticatedRequest[_] => authenticated(ar)
    case ur: UnauthenticatedRequest[_] => unauthenticated(ur)
  }

  def apply(request: CanBeAuthenticatedRequest[_])
           (authenticated: (AuthenticatedRequest[_]) => Int)
           (unauthenticated: (UnauthenticatedRequest[_]) => Int): Int = {
    fold(authenticated)(unauthenticated)(request)
  }

  def async(request: CanBeAuthenticatedRequest[_])
           (authenticated: (AuthenticatedRequest[_]) => Option[Int])
           (unauthenticated: (UnauthenticatedRequest[_]) => Option[Int]): Option[Int] = {
    fold(authenticated)(unauthenticated)(request)
  }
}

但是,如果我更改,则此代码不再编译:
fold(authenticated)(unauthenticated)(request)fold(authenticated)(unauthenticated).apply(request)(我添加了对 apply() 的显式调用)它编译。为什么会这样?在一个类上调用 () 和 .apply() 应该是一样的,不是吗?

编译器似乎要求将返回类型(Int 或 Option[Int])传递给 PartialFunction,而不是 CanBeAuthenticatedRequest 类型。


因为您在 `fold[T : Helper]\\' 中定义了一个上下文绑定,编译器将添加另一个参数列表。换句话说,上下文绑定只是语法糖:

1
2
3
private def fold[T](authenticated: (AuthenticatedRequest[_]) => T)
                   (unauthenticated: (UnauthenticatedRequest[_]) => T)
                   (implicit helper: Helper[T): PartialFunction[CanBeAuthenticatedRequest[_], T]

所以当你调用

1
fold(authenticated)(unauthenticated)(request)

编译器认为 request 应该是显式指定的隐式 Helper[T].