St.Antario
St.Antario

Reputation: 27425

Pattern match scala against set

I wrote the following code:

class MyActor extends Actor {
  override def receive: Receive = {
    case p: Set[String] => //
  }
}

But when compiling the following warning is emitted:

Warning:(22, 13) non-variable type argument String in type pattern 
scala.collection.immutable.Set[String] (the underlying of Set[String]) 
is unchecked since it is eliminated by erasure
    case p: Set[String] =>

Why? Is there a way to get rid of it except supressing?

Upvotes: 1

Views: 438

Answers (1)

Luka Jacobowitz
Luka Jacobowitz

Reputation: 23532

You can't really pattern match on anything with a type parameter, this is by design, since the JVM runtime does not have the notion of type parameters at runtime.

Easiest thing to do is wrap it in a value class.

case class StringSet(val value: Set[String]) extends AnyVal

Then you can easily pattern match on it:

override def receive: Receive = {
  case StringSet(p) => //
}

Upvotes: 1

Related Questions