Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala cats和遍历-dons';不编译_Scala_Applicative_Scala Cats - Fatal编程技术网

Scala cats和遍历-dons';不编译

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

我试图使用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]] 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]                                                                   ^