2016-09-13 63 views
0

在scala7.0.6:下列程序工作:進口scalaz.syntax.traverse._不起作用

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

當您導入Scalaz._和​​同一範圍內:

<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._和​​不同(嵌套)進口作用域沒有衝突,後者的陰影前者。或者你可以完全放棄import scalaz.syntax.traverse._,它會編譯。

scalac有標記-Xlog-implicits這有助於。