我对以下代码发出警告:
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
案例类的类型变量可以是KafkaHealthEvent
或SapHealthEvent
。
答案 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