不合理的模式匹配可能不是详尽的警告

问题描述

在学习期间,我遇到了我认为不合理的警告。 我有单独的代码片段来演示它。

简而言之,我有

  1. 密封特征以允许编译器进行详尽匹配
sealed trait Shape {
  def sides: Int
  def perimeter: Double
  def area: Double
  def colour: Colour
}
  1. 最终案例类实现也用于穷举匹配
sealed trait Rectangular extends Shape {
  override def sides: Int = 4
  override def perimeter: Double = 2 * width + 2 * height
  override def area: Double = width * height
  def width: Double
  def height: Double
}
case class Circle(radius: Double,colour: Colour) extends Shape {
  override def perimeter: Double = 2.0 * math.Pi * radius
  override def area: Double = math.Pi * radius * radius
  override val sides: Int = 0
}
case class Square(sideLength: Double,colour: Colour) extends Rectangular {
  val width = sideLength
  val height = sideLength
}
  1. 最后是模式匹配(当然 => 之后的逻辑被简化了,因为匹配才是最重要的):
case object Draws {
  def apply(shape: Shape): String = shape match {
    case Square(_,predef) => predef.toString
    case Rectangle(_,_,predef) => predef.toString
    case Circle(_,predef) => predef.toString
  }
}

我真的被警告震惊了:

match may not be exhaustive.
It would fail on the following inputs: Circle(_,_),Rectangle(_,Square(_,_
def apply(shape: Shape): String = shape match {

据我所知,我的匹配涵盖了 Circle(_,_) 之类的情况 - 我只是将最后一个属性绑定到 predef 变量。由于我穷尽了所有可能性,因此也应该涵盖 case _。

谁能帮我解释一下为什么特别是这可能会以 MatchError 结束?我自己也搞不清楚。

关于, 迈克尔

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)