模式匹配scala反对集

时间:2017-03-22 15:38:01

标签: scala collections

我写了以下代码:

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

但是在编译以下警告时会发出:

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] =>

为什么呢?除了压抑之外,有没有办法摆脱它?

1 个答案:

答案 0 :(得分:1)

您无法通过类型参数对任何内容进行模式匹配,这是设计使然,因为JVM运行时在运行时没有类型参数的概念。

最简单的事情是将它包装在一个值类中。

 System.TimeoutException: Timeout performing GET netkey, inst: 6, queue: 10, qu: 0, qs: 10, qc: 0, wr: 0, wq: 0, in: 645, ar: 0, clientName: XXXXXX, serverEndpoint: Unspecified/XXXXXXX, keyHashSlot: XXXXX(Please take a look at this article for some common client-side issues that can cause timeouts: https://github.com/StackExchange/StackExchange.Redis/tree/master/Docs/Timeouts.md)
   at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server)
   at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server)
   at StackExchange.Redis.RedisDatabase.StringGet(RedisKey key, CommandFlags flags)

然后你可以很容易地模仿匹配:

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