在对象外部导入scalaz.syntax.traverse._不起作用

时间:2016-09-13 11:47:21

标签: scala scalaz

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

为什么会这样?

1 个答案:

答案 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有帮助。