1

我有以下代码功能:

  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

4

1 回答 1

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文档

于 2018-01-14T20:09:12.340 回答