我花了一段时间来研究这个,但仍然无法让类型系统同意我的观点,即这种抽象实际上是一个
ObjectMapper => A => Either[Throwable, B]
我目前的类型看起来像
import cats._
import cats.data.{Reader, Kleisli, EitherT}
import cats.implicits._
import com.fasterxml.jackson.databind.{ObjectMapper, JsonNode}
type JsonParser[A, B] = Kleisli[EitherT[Reader[A, ?], Throwable, ?], ObjectMapper, B]
为了测试实例化这种类型,我编写了一个非常简单的函数,它接受一个 jsonString
并返回一个Either[Throwable, JsonNode]
val makeJsonNode: JsonParser[String, JsonNode] =
Kleisli(mapper => EitherT(Reader(json => tryToEither(Try(mapper.readTree(json))))))
我收到此错误消息:
[info] Compiling 6 Scala sources to .../scala-2.11/classes...
[error] Test.scala:140: missing parameter type
[error] Kleisli(mapper => EitherT(Reader(json => tryToEither(Try(mapper.readTree(json))))))
[error] ^
[error] Test.scala:140: missing parameter type
[error] Kleisli(mapper => EitherT(Reader(json => tryToEither(Try(mapper.readTree(json))))))
[error] ^
[error] Test.scala:140: no type parameters for method apply: (value: F[Either[A,B]])cats.data.EitherT[F,A,B] in object EitherT exist so that it can be applied to arguments (cats.data.Reader[Any,Nothing])
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : cats.data.Reader[Any,Nothing]
[error] (which expands to) cats.data.Kleisli[cats.Id,Any,Nothing]
[error] required: ?F[Either[?A,?B]]
[error] Kleisli(mapper => EitherT(Reader(json => tryToEither(Try(mapper.readTree(json))))))
[error] ^
[error] Test.scala:140: type mismatch;
[error] found : cats.data.Reader[Any,Nothing]
[error] (which expands to) cats.data.Kleisli[cats.Id,Any,Nothing]
[error] required: F[Either[A,B]]
[error] Kleisli(mapper => EitherT(Reader(json => tryToEither(Try(mapper.readTree(json))))))
[error] ^
[error] Test.scala:140: no type parameters for method apply: (run: A => F[B])cats.data.Kleisli[F,A,B] in object Kleisli exist so that it can be applied to arguments (<error> => cats.data.EitherT[F,A,B])
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : <error> => cats.data.EitherT[F,A,B]
[error] required: ?A => ?F[?B]
[error] Kleisli(mapper => EitherT(Reader(json => tryToEither(Try(mapper.readTree(json))))))
[error] ^
[error] Test.scala:140: type mismatch;
[error] found : cats.data.Kleisli[F,A,B]
[error] required: Test.this.Parser[String,com.fasterxml.jackson.databind.JsonNode]
[error] (which expands to) cats.data.Kleisli[[γ$1$]cats.data.EitherT[[β$0$]cats.data.Kleisli[[A]A,String,β$0$],Throwable,γ$1$],com.fasterxml.jackson.databind.ObjectMapper,com.fasterxml.jackson.databind.JsonNode]
[error] Kleisli(mapper => EitherT(Reader(json => tryToEither(Try(mapper.readTree(json))))))
[error] ^
[error] 6 errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 2 s, completed Jun 16, 2017 1:28:17 PM
我意识到这种类型有点麻烦,并且肯定可以改进(并且可能是矫枉过正),但我现在正在深入研究这个,以了解更多关于 Cats 和 Scala 类型系统的信息,以便我自己的个人成长。Scala 试图告诉我什么是错误的,在这里,我应该如何解决这个问题?