问号是什么意思?

时间:2018-01-14 20:00:32

标签: scala kind-projector

我有以下代码函数:

  def jsonOrBadRequest[F[_] : Monad](service: HttpService[F])
  : HttpService[F]
  = {
    object dsl extends Http4sDsl[F]
    import dsl._
    Kleisli[OptionT[F, ?], Request[F], Response[F]] { req =>
      req.contentType match {
        case Some(s) =>
          if (s != `Content-Type`(MediaType.`application/json`))
            OptionT.liftF(BadRequest("Malformed format."))
          else
            service(req)
        case None =>
          OptionT.liftF(BadRequest("Malformed format."))
      }
    }
  }

想知道问号是什么意思?它来自图书馆https://github.com/non/kind-projector

1 个答案:

答案 0 :(得分:3)

这不是特殊的Scala语法。 ?是一个有效的标识符,就像其他任何东西一样。 kind-projector使用它来声明类型级lambda。例如,

Tuple2[?, Double]        // equivalent to: type R[A] = Tuple2[A, Double]
Either[Int, +?]          // equivalent to: type R[+A] = Either[Int, A]
Function2[-?, Long, +?]  // equivalent to: type R[-A, +B] = Function2[A, Long, B]
EitherT[?[_], Int, ?]    // equivalent to: type R[F[_], B] = EitherT[F, Int, B]

(直接取自kind-projector documentation

的例子