David Portabella David Portabella - 2 months ago 8
Scala Question

import scalaz.syntax.traverse._ outside the object does not work

On scala7.0.6: the following program works:

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))
}


However, if I move the
import scalaz.syntax.traverse._
outside the object
Playground
, on top of the file, then I get:

Error: value traverse is not a member of List[Int]


why is that?

Answer

When you import Scalaz._ and scalaz.syntax.traverse._ within the same scope:

<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))

If Scalaz._ and scalaz.syntax.traverse._ are imported in different (nested) scopes there is no conflict, the latter shadows the former. Or you can completely drop import scalaz.syntax.traverse._, it will compile.

scalac has flag -Xlog-implicits which helps.