문제

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?

도움이 되었습니까?

해결책 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) => {
  //.....
  //.....
}

다른 팁

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) =>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top