Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mutable and immutable Sets in the same file, in Scala

Tags:

set

scala

Given that default implementation of a Set is immutable:

val Set = immutable.Set

And in order to make it mutable one needs to import

import scala.collection.mutable.Set;

In event one needs to use both mutable and immutable Sets in a given file, how should one go about it?

like image 859
James Raitsev Avatar asked Jan 06 '13 19:01

James Raitsev


2 Answers

When you need to use both mutable and immutable collections in the same file, the canonical solution is just to prefix with mutable or immutable explicitly.

import collection._

val myMutableSet: mutable.Set[Int] = mutable.Set(1, 2, 3)
val myImmutableSet: immutable.Set[Int] = immutable.Set(1, 2, 3)

AS Kim Stebel mentioned in his answer, you can also use a renaming import:

import scala.collection.mutable.{Set => MutableSet}

However mutable.Set is only one character more than MutableSet, and does not introduce any new name so you might as well just use the former form.

like image 107
Régis Jean-Gilles Avatar answered Nov 02 '22 20:11

Régis Jean-Gilles


You can rename symbols when you import them.

import scala.collection.mutable.{Set => MutableSet}
like image 23
Kim Stebel Avatar answered Nov 02 '22 22:11

Kim Stebel