如何在Scala Cats中创建EitherT的Kleisli实例?

时间:2017-06-16 18:37:00

标签: scala instantiation monad-transformers scala-cats kleisli

我花了一段时间来攻击这个并且仍然不能让类型系统同意我这个抽象真的是

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]

为了测试实例化这个类型,我编写了一个非常简单的函数,它接受一个json String并返回一个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试图告诉我这是错的,在这里,我应该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

基本问题是缺少参数类型,请尝试

Kleisli((mapper: ObjectMapper) => EitherT(Reader((json: String) => tryToEither(Try(mapper.readTree(json))))))

当存在提供此信息的预期类型时,您只能使用lambda而不指定参数类型,在这种情况下makeJsonNode: JsonParser[String, JsonNode]似乎不够。