首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将整型变压器提升到单台变压器

将整型变压器提升到单台变压器
EN

Stack Overflow用户
提问于 2018-07-12 02:44:36
回答 1查看 520关注 0票数 1

我有这3台单台变压器

代码语言:javascript
复制
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])到它。

我写了这段代码,它会编译。但我需要一个更简洁的方法来实现同样的目标。

代码语言:javascript
复制
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的事情很烦人。但如果我把最后一行改为

代码语言:javascript
复制
val z : P[Int] = OptionT(EitherT(Future(Right(Option(10)))))

我得到了这个编译器错误

代码语言:javascript
复制
[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
>
EN

回答 1

Stack Overflow用户

发布于 2018-07-12 03:56:27

尝试为右提供类型对齐:

代码语言:javascript
复制
val z : P[Int] = OptionT(EitherT(Future(Right[String,Option[Int]](Option(10)))))

如果没有类型参数,那么当您执行Right(1)时,scala会推断出Either[Nothing,Int]

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51296610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档