Method value apply with alternatives cannot be applied

I don’t understand why the compiler thinks this method call is ambiguous. Or am I misinterpreting the compiler message?

Isn’t it the case that my call Assignment(Set()) is unambiguous?

Error:(257, 42) overloaded method value apply with alternatives:
  (trueVariables: Set[Int])bdd.Assignment <and>
  (bitMask: Long)bdd.Assignment
 cannot be applied to (scala.collection.Set[Int])
    println("a+b =" + Or(Bdd(1), Bdd(2))(Assignment(Set(1))))

Here is the call-site


  test("printer") {
    import Bdd._

    println("a+b =" + Or(Bdd(1), Bdd(2))(Assignment(Set(1))))
    println("a+b =" + Or(Bdd(1), Bdd(2))(Assignment(Set(2))))
    println("a+b =" + Or(Bdd(1), Bdd(2))(Assignment(Set(1, 2))))
    println("a+b =" + Or(Bdd(1), Bdd(2))(Assignment(Set[Int]())))

    println("a&b =" + And(Bdd(1), Bdd(2))(Assignment(Set(1))))
    println("a&b =" + And(Bdd(1), Bdd(2))(Assignment(Set(2))))
    println("a&b =" + And(Bdd(1), Bdd(2))(Assignment(Set(1, 2))))
    println("a&b =" + And(Bdd(1), Bdd(2))(Assignment(Set[Int]())))

    val bdd = Or(Bdd(1), Not(Bdd(2)))
    println("xnor" + Xnor(bdd, bdd))
  }

And here are the definitions of Assignment



package bdd

case class Assignment(trueVariables:Set[Int]) {
  def value(v:Int):Boolean = {
    trueVariables.contains(v)
  }
}

object Assignment {
  def apply(bitMask: Long): Assignment = {
    def recur(bitMask: Long, bit: Int, set: Set[Int]): Set[Int] = {
      if (0 == bitMask)
        set
      else if (0 == bitMask % 2)
        recur(bitMask / 2, bit + 1, set)
      else
        recur(bitMask / 2, bit + 1, set + bit)
    }
    Assignment(recur(bitMask, 1, Set()))
  }
}

Different types with simple name “Set” - scala.collection.Set and something else…?

Yes, it seems to be confused between scala.collection.Set and scala.collection.immutable.Set. That’s really bizarre.

Not that bizarre. collection.Set is a supertype of both collection.mutable.Set and collection.immutable.Set. If the method explicitly requires a collection.immutable.Set, the compiler must not accept an argument of static type collection.Set, since that might actually be an instance of collection.mutable.Set - just as it mustn’t accept an argument of static type Any, since this might be, well, anything. :slight_smile:

You are right of course, but I’ve never encountered this problem before. Is this the first time I’ve ever written a method whose argument is declare as type Set[Int] ?

Some explicit import scala.collection.immutable._ in the Assignment source file, maybe?

If you use unqualified Set without explicitly importing mutable or immutable, you’ll get Predef.Set, which is an alias for collections.Set.

1 Like

Yes I was importing scala.collection.mutable to get mutable maps. Thanks for the clue.