Scala cats и travers синтаксис для Either - не компилируется - PullRequest
0 голосов
/ 07 сентября 2018

Я пытаюсь использовать traverse (или sequence, что довольно похоже на мою задачу) из библиотеки кошек https://typelevel.org/cats/typeclasses/traverse.html. Я хочу пройти List[A] с помощью функции A => Either[L,R], чтобы получить Either[L,List[R]] в результате.

Рассмотрим следующий крошечный пример (я использую scala-2.12.6, cats-core-1.3.1, sbt-1.1.2):

import cats.implicits._

def isOdd(i: Int): Either[String, Int] = 
  if (i % 2 != 0) Right(i) else Left("EVEN")

val odd: Either[String, List[Int]] = (1 to 10).toList.traverse(isOdd)

Не компилируется, выдает:

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]                                                                   ^

1 Ответ

0 голосов
/ 07 марта 2019

Требуется флаг компилятора частичного объединения. В скале 2.12

добавить scalacOptions += "-Ypartial-unification" в build.sbt

Спасибо, Томас

...