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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With