在scala7.0.6上:以下程序有效:
import scalaz._, Scalaz._
object Playground extends App {
val smallNumbers = List(1,2,3,4,5)
val doubleSmall: Int => Option[Int] = x => if (x < 30) Some(x * 2) else None
import scalaz.syntax.traverse._
println(smallNumbers.traverse(doubleSmall))
}
但是,如果我将import scalaz.syntax.traverse._
移到对象Playground
之外,在文件顶部,那么我得到:
Error: value traverse is not a member of List[Int]
为什么会这样?
答案 0 :(得分:1)
在同一范围内导入Scalaz._
和scalaz.syntax.traverse._
时:
<ToTraverseOps: error> is not a valid implicit value for
Wrapper.Playground.smallNumbers.type => ?{def traverse: ?} because:
reference to ToTraverseOps is ambiguous;
it is imported twice in the same scope by
import scalaz.syntax.traverse._
and import Scalaz._
println(smallNumbers.traverse(doubleSmall))
如果Scalaz._
和scalaz.syntax.traverse._
在不同(嵌套)范围内导入,则不存在冲突,后者会影响前者。或者您可以完全删除import scalaz.syntax.traverse._
,它将编译。
scalac
标志-Xlog-implicits
有帮助。