Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala mutable collections and "Reference must be prefixed warnings"

I have to use a mutable linked list for a specific use case. However I'd like to avoid "Reference must be prefixed" warnings.

Aliasing the import seems to be a solution:

import scala.collection.mutable.{LinkedList => MutableLinkedList}

it works on most cases except in pattern matching an empty LinkedList, this still produces the warning:

case MutableLinkedList() => // do Something

the only way I can remove this warning seems to be to do a fully qualified case check on an empty list:

case scala.collection.mutable.LinkedList() => // do Something

Why does the first case not get rid of the warning?

like image 533
ArtisanV Avatar asked Aug 12 '13 20:08

ArtisanV


2 Answers

Just import mutable package:

import collection.mutable

and use it with any mutable collection:

mutable.LinkedList(1, 2, 3)

or if you prefer more concise variant:

import collection.{mutable => m}
m.LinkedList(1, 2, 3)

It will work with pattern matching also.

like image 134
Sergey Passichenko Avatar answered Nov 17 '22 03:11

Sergey Passichenko


I tried it in the 2.10.2 shell and didn't see any warnings.

One way of "aliasing" the mutable.LinkedList extractor is doing:

scala> MutableLinkedList(1,2,3,4,5)
res0: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5)

scala> val LL = MutableLinkedList
LL: scala.collection.mutable.LinkedList.type = scala.collection.mutable.LinkedList$@5798795f

scala> res0 match { case LL(1,2, _*) => "yey"; case _ => "bad" }
res3: String = yey

See? Now LL points to the MutableLinkedList companion object

like image 29
pedrofurla Avatar answered Nov 17 '22 05:11

pedrofurla