1
任何人都可以告诉我,如果在Scala中可以使用以下语法行吗?对于理解类型检查
@annotation.tailrec
def traverse[E,A,B](es: List[A])(f: A => Either[E, B]): Either[E, List[B]] = {
def go(es: List[A], rs: Either[E, List[B]]): Either[E, List[B]] = {
es match {
case Nil => rs
case x::xs => for {
Right(b) <- f(x);
Right(ls) <- rs
} yield go(xs, Right(b::ls))
}
}
go(es, Right(List()))
}
我一直得到下面的语法异常
Error:(47, 12) constructor cannot be instantiated to expected type;
found : A$A400.this.Right[A]
required: List[?B3] where type ?B3 <: B (this is a GADT skolem)
Right(ls) <- rs
^
尝试将rs匹配为Left还是Right? – Ashalynd
多数民众赞成我正在努力实现,但我得到的例外 – MrX
我认为不太可能的编译器将解决你的函数是tailrecursive。 for将解析成flatmap/Map并作为函数传递yield。它要求编译器稍微调整一下,这是一个尾部调用。 –