How to disambiguate type of same name

Can someone help me make the right type declaration?

I’d like an occurrence of the word Map in my program to mean scala.collection.concurrent.Map rather than scala.collection.immutable.Map. I’m not sure how to do this.

The following code gives a compilation error.

  def main(argv:Array[String]):Unit = {
    import scala.collection.concurrent.Map
    trait Trait1
    trait Trait2
    val tds = Seq(SEmpty,
                  STop,
                  SAtomic(classOf[Trait1]),
                  SNot(classOf[Trait1]),
                  SEql(42),
                  SMember(1,2,3),
                  evenType, // SCustom(evenp)
                  SAnd(classOf[Trait1],classOf[Trait2]),
                  SOr(classOf[Trait1],classOf[Trait2]))

    val tdToInt = Map[SimpleTypeD,Int]()

    tds.foreach{td => println(SimpleTypeDtoBdd(td,tdToInt))} // compilation error on this line
  }

/Users/jimka/Repos/regular-type-expression/cl-robdd-scala/src/main/scala/genusbdd/GenusBdd.scala:64:51
type mismatch;
 found   : Map[genus.SimpleTypeD,Int] (in scala.collection.immutable) 
 required: Map[genus.SimpleTypeD,Int] (in scala.collection.concurrent) 
    tds.foreach{td => println(SimpleTypeDtoBdd(td,tdToInt))}

And if I change the declaration as follows, I get a different compiler error.

  def main(argv:Array[String]):Unit = {
    import scala.collection.concurrent.Map
    trait Trait1
    trait Trait2
    val tds = Seq(SEmpty,
                  STop,
                  SAtomic(classOf[Trait1]),
                  SNot(classOf[Trait1]),
                  SEql(42),
                  SMember(1,2,3),
                  evenType, // SCustom(evenp)
                  SAnd(classOf[Trait1],classOf[Trait2]),
                  SOr(classOf[Trait1],classOf[Trait2]))

    val tdToInt = scala.collection.concurrent.Map[SimpleTypeD,Int]() // compilation error on this line

    tds.foreach{td => println(SimpleTypeDtoBdd(td,tdToInt))}
  }

/Users/jimka/Repos/regular-type-expression/cl-robdd-scala/src/main/scala/genusbdd/GenusBdd.scala:63:47
object Map is not a member of package scala.collection.concurrent
Note: trait Map exists, but it has no companion object.
    val tdToInt = scala.collection.concurrent.Map[SimpleTypeD,Int]()

Actually what would be even better, is that I have a very small number of places in my code where I need a mutable map. I’d like those to be mutable, and all other Maps to be immutable, unless I specify otherwise. How can I have mutable and immutable maps within the same function/class?

Answered on gitter.
The problem was that I need to use scala.collection.mutable.Map rather than scala.collection.concurrent.Map.

What I do is import scala.collection.mutable and then write mutable.Map wherever I need to say mutable Map. Is something more sophisticated needed?

regards,
Siddhartha

2 Likes

Another option might be import aliases. E.g.

import scala.collection.{mutable => mut}
// ...
val m = mut.Map()

or

import scala.collection.mutable.{Map => MutMap}
// ...
val m = MutMap()
1 Like