类型参数避免匹配穷举性警告

问题描述 投票:1回答:1

为什么密封类型绑定的类型参数看起来not会引发穷举性警告

sealed trait A
case class B() extends A
case class C(i: Option[Int]) extends A

def f[T <: A](a: T) =
  a match {
    case B() =>
    case C(None) =>
  }

f(C(Some(42))) // throws MatchError

没有类型参数的想法

def f(a: A) =
  a match {
    case B() =>
    case C(None) =>
  }

发出警告

warning: match may not be exhaustive.
It would fail on the following input: C(Some(_))
    a match {
    ^
scala pattern-matching compiler-warnings type-parameter non-exhaustive-patterns
1个回答
0
投票

模式匹配

def f[T <: A](a: T) =
  a match {
    case B() =>
    case C(None) =>
  }
© www.soinside.com 2019 - 2024. All rights reserved.