Scala cats和遍历-dons';不编译
我试图使用cats库中的Scala cats和遍历-dons';不编译,scala,applicative,scala-cats,Scala,Applicative,Scala Cats,我试图使用cats库中的遍历(或序列,这与我的任务几乎相同)。我想用函数a=>or[L,R]遍历一个List[a],得到or[L,List[R] 考虑以下小例子(我使用scala-2.12.6、cats-core-1.3.1、sbt-1.1.2): 它不编译,它给出: no type parameters for method traverse: (f: Int => G[B])(implicit evidence$1: cats.Applicative[G])G[List[B]] exi
遍历
(或序列
,这与我的任务几乎相同)。我想用函数a=>or[L,R]
遍历一个List[a]
,得到or[L,List[R]
考虑以下小例子(我使用scala-2.12.6、cats-core-1.3.1、sbt-1.1.2):
它不编译,它给出:
no type parameters for method traverse: (f: Int => G[B])(implicit evidence$1: cats.Applicative[G])G[List[B]] exist so that it can be applied to arguments (Int => Either[String,Int])
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : Int => Either[String,Int]
[error] required: Int => ?G[?B]
[error] val odd: Either[String, List[Int]] = (1 to 10).toList.traverse(isOdd)
type mismatch;
[error] found : Int => Either[String,Int]
[error] required: Int => G[B]
[error] val odd: Either[String, List[Int]] = (1 to 10).toList.traverse(isOdd)
[error] ^
could not find implicit value for evidence parameter of type cats.Applicative[G]
[error] val odd: Either[String, List[Int]] = (1 to 10).toList.traverse(isOdd)
[error] ^
部分统一编译器标志是必需的。在scala 2.12中 在
build.sbt
谢谢Thomas(1到10)。toList.traverse[({typel[A]=任一[String,A]})#L,Int](isOdd)工作正常。可以使用种类投影仪将其缩短为
(1到10)。toList.Travel[字符串、],Int](isOdd)
,也可以使用类型别名。如果没有指定的类型,我不知道如何使它工作。一般的问题是,编译器并没有正确地在那个里进行推断,因为它有两个洞,而且更难。之前有一个经常有用的变体,但是它被删除了。非常感谢,它确实有效。为什么你的回答是评论,而不是回答?实际上,使用类型别名更容易阅读,比如type EitherString[R]=String,R]
或其他东西而不是String
。我可以在这里编译(1到10).toList.traverse(isOdd)
。您确定已启用部分统一吗?(将scalacOptions+=“-y部分统一”
添加到buildld.sbt,请参阅)
no type parameters for method traverse: (f: Int => G[B])(implicit evidence$1: cats.Applicative[G])G[List[B]] exist so that it can be applied to arguments (Int => Either[String,Int])
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : Int => Either[String,Int]
[error] required: Int => ?G[?B]
[error] val odd: Either[String, List[Int]] = (1 to 10).toList.traverse(isOdd)
type mismatch;
[error] found : Int => Either[String,Int]
[error] required: Int => G[B]
[error] val odd: Either[String, List[Int]] = (1 to 10).toList.traverse(isOdd)
[error] ^
could not find implicit value for evidence parameter of type cats.Applicative[G]
[error] val odd: Either[String, List[Int]] = (1 to 10).toList.traverse(isOdd)
[error] ^