1

I have an Akka Actor that has the following case pattern match check in its receive method as below:

def receive = {
  case x: (String, ListBuffer[String]) if(x._2.size >= 0) => {
  .....
  .....
}

When I compile, I get to see the following compiler warnings:

warning: non-variable type argument String in type pattern (String, scala.collection.mutable.ListBuffer[String]) 
is unchecked since it is eliminated by erasure)

Any clues as to how I could get rid of them? I do not want to set the compiler settings to ignore these warnings, but I do not see a reason why the compiler issues a warning?

joescii
  • 6,495
  • 1
  • 27
  • 32
joesan
  • 13,963
  • 27
  • 95
  • 232
  • 1
    Scala only sees from x during runtime that it is a Tuple2, so if a Tuple2 comes with different type parameters, that might be a real problem there. – Gábor Bakos Mar 10 '14 at 17:15

2 Answers2

3

This is due to the JVM's type erasure. At runtime, the JVM only sees ListBuffer[Any]. Static type information of generics is lost. If you don't care about the generic type of the ListBuffer, you can change the pattern match to:

case x: (String, ListBuffer[_]) if(x._2.size >= 0) =>
Dia Kharrat
  • 5,948
  • 3
  • 32
  • 43
  • This would mean that I have to cast x._2 to a ListBuffer[String] before I could use it. Not so ideal! – joesan Mar 11 '14 at 08:14
2

One little trick I like to use for this problem is type aliasing.

type MyBuffer = ListBuffer[String]

//...

def receive = {
  case x: (String, MyBuffer) if(x._2.size >= 0) => {
  //.....
  //.....
}
joescii
  • 6,495
  • 1
  • 27
  • 32