2017-06-16 51 views
0

我已经花了,而黑客在此,仍然不能完全得到该型系统与我同意这种抽象是一个真正的如何在Scala猫中构建一个EitherT的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类型系统,斯卡拉试图告诉我什么是错误的,在这里,我该如何解决这个问题?

+0

玩了一会之后,我觉得类型删除可能是罪魁祸首。我是否在这个猜想的正确轨道上? – josiah

回答

0

基础的问题是缺少的参数类型,尝试

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

您只能使用lambda没有指定参数类型时,有一个预期的类型,给这个信息,在这种情况下makeJsonNode: JsonParser[String, JsonNode]似乎并不够了。

相关问题