消息[Playground.this.KafkaHealthEvent]未选中,因为它已通过擦除消除

时间:2019-07-17 13:17:20

标签: scala

我对以下代码发出警告:

sealed trait KafkaHealthEvent
case object KafkaInactiveApproved extends KafkaHealthEvent
case object KafkaActiveApproved extends KafkaHealthEvent


sealed trait SapHealthEvent
case object SapInactiveApproved extends SapHealthEvent
case object SapActiveApproved extends SapHealthEvent


sealed trait ServerHealthStreamer

case object Complete extends ServerHealthStreamer
case class Message[A](msg: A) extends ServerHealthStreamer

val msg: ServerHealthStreamer = Message[KafkaHealthEvent](KafkaActiveApproved)

msg match {
  case Complete => 
    println("Complete")
  case m: Message[KafkaHealthEvent] =>
    println(m.msg)  
}

,警告是:

non-variable type argument Playground.this.KafkaHealthEvent in type pattern Playground.this.Message[Playground.this.KafkaHealthEvent] is unchecked since it is eliminated by erasure  

如何解决问题? Message案例类的类型变量可以是KafkaHealthEventSapHealthEvent

1 个答案:

答案 0 :(得分:1)

只需调整模式匹配即可:

msg match {
  case Complete => 
    println("Complete")
  case Message(msg:KafkaHealthEvent) =>
    println(msg)  
  case other => // handle unexpected
}

这是一个博客,解释类型擦除:http://squidarth.com/scala/types/2019/01/11/type-erasure-scala.html