我有这3台单台变压器
type T[A] = OptionT[Future, A]
type E[A] = EitherT[Future, String, A]
type P[A] = OptionT[E, A]我想要提升相应的完整类型(意思是确切的对应类型)到这些。所以对于T,我想把Future[OptionInt]提升到它里面。对于E,我想要提升未来(EitherString,Int),对于P,我想要提升(未来[字符串,OptionInt])到它。
我写了这段代码,它会编译。但我需要一个更简洁的方法来实现同样的目标。
val x : T[Int] = OptionT(Future(Option(10)))
val y : E[Int] = EitherT(Future(Right(10).asInstanceOf[Either[String, Int]]))
val z : P[Int] = OptionT(EitherT(Future(Right(Option(10)).asInstanceOf[Either[String, Option[Int]]])))我使用的是Cats 1.1.0和Scala2.12.3。
asInstanceOf的事情很烦人。但如果我把最后一行改为
val z : P[Int] = OptionT(EitherT(Future(Right(Option(10)))))我得到了这个编译器错误
[info] Compiling 1 Scala source to
[error] /Users//code/dallasscalacats/src/main/scala/com//Transformers.scala:32: 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 (scala.concurrent.Future[scala.util.Right[Nothing,Option[Int]]])
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : scala.concurrent.Future[scala.util.Right[Nothing,Option[Int]]]
[error] required: ?F[Either[?A,?B]]
[error] val z : P[Int] = OptionT(EitherT(Future(Right(Option(10)))))
[error] ^
[error] /Users//code/dallasscalacats/src/main/scala/com//Transformers.scala:32: type mismatch;
[error] found : scala.concurrent.Future[scala.util.Right[Nothing,Option[Int]]]
[error] required: F[Either[A,B]]
[error] val z : P[Int] = OptionT(EitherT(Future(Right(Option(10)))))
[error] ^
[error] /Users//code/dallasscalacats/src/main/scala/com//Transformers.scala:32: type mismatch;
[error] found : cats.data.EitherT[F,A,B]
[error] required: com.abhi.Transformers.E[Option[Int]]
[error] (which expands to) cats.data.EitherT[scala.concurrent.Future,String,Option[Int]]
[error] val z : P[Int] = OptionT(EitherT(Future(Right(Option(10)))))
[error] ^
[error] three errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 0 s, completed Jul 11, 2018 9:46:21 PM
>发布于 2018-07-12 03:56:27
尝试为右提供类型对齐:
val z : P[Int] = OptionT(EitherT(Future(Right[String,Option[Int]](Option(10)))))如果没有类型参数,那么当您执行Right(1)时,scala会推断出Either[Nothing,Int]。
https://stackoverflow.com/questions/51296610
复制相似问题