Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a List of (immutable and mutable) Sets in scala?

I try to build a list of (mutable and immutable) Sets. The compiler gets into trouble as it cannot figure out the type of that list. I always thought that I can connect Lists of any types and that the type of the new List is a kind of supertype of the connected Lists. In the following example, I define some lists. You can see the types of those lists, given by the compiler:

val intList = List(1) //List[Int]
val stringList = List("ab") //List[java.lang.String]
val mSetList = List(mutable.Set(1, 2, 3)) //List[scala.collection.mutable.Set[Int]]
val iSetList = List(immutable.Set(1, 2, 3)) //List[scala.collection.immutable.Set[Int]]

Now I use the ::: operator to connect these lists:

val intStringList = intList:::stringList //List[Any]
val intMSetList = intList:::mSetList //List[Any]
val intISetList = intList:::iSetList //List[Any]

As expected, the compiler computes a common supertype (List[Any]) of both lists. But the following does not compile:

val iSetmSetList = iSetList:::mSetList //type error

But if I explicitly "cast" the two lists, it works:

val setList1 : List[scala.collection.Set[Int]] = mSetList //List[scala.collection.Set[Int]]
val setList2 : List[scala.collection.Set[Int]] = iSetList // List[scala.collection.Set[Int]]
val setList = setList1:::setList2 //List[scala.collection.Set[Int]]

Why do I have to help the compiler to get the correct type of that list? And why does it produce an error rather than simply type it with List[Any]? Is it theoretically impossible to compute the type List[scala.collection.Set[Int]] or is it a kind of bug in the compiler?

Thanks a lot for your answers :-)

like image 665
Jan Avatar asked Jul 15 '11 13:07

Jan


People also ask

Is list immutable in Scala?

Specific to Scala, a list is a collection which contains immutable data, which means that once the list is created, then it can not be altered. In Scala, the list represents a linked list. In a Scala list, each element need not be of the same data type.

How do I create a mutable list in Scala?

Because a List is immutable, if you need to create a list that is constantly changing, the preferred approach is to use a ListBuffer while the list is being modified, then convert it to a List when a List is needed. The ListBuffer Scaladoc states that a ListBuffer is “a Buffer implementation backed by a list.

What are mutable and immutable collections in Scala?

Scala collections systematically distinguish between mutable and immutable collections. A mutable collection can be updated or extended in place. This means you can change, add, or remove elements of a collection as a side effect. Immutable collections, by contrast, never change.

Which method returns an immutable set consisting of all elements of this immutable set except the first one?

def tail: Set[A] Returns a immutable set consisting of all elements of this immutable set except the first one.


1 Answers

It was a bug, and is fixed in nightly versions, as huynhjl suspected:

Welcome to Scala version 2.10.0.r25234-b20110705020226
  (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24)
Type in expressions to have them evaluated.
Type :help for more information.
. . .
scala> val iSetmSetList = iSetList:::mSetList //type error
iSetmSetList: List[scala.collection.Set[Int]] = List(Set(1, 2, 3), Set(2, 1, 3))
like image 169
Rex Kerr Avatar answered Oct 16 '22 13:10

Rex Kerr