Scala:当针对密封特征进行模式匹配时,编译器不会发出警告

Scala : Compiler emits no warning when pattern matching against a sealed trait

这是一个片段。模式匹配时,编译器不会发出警告。您知道任何解决方法吗?

我希望编译器在我忘记针对 SimpleExpr.ExprOtherExpr.Expr 进行模式匹配时发出警告。此构造允许我分解两个表达式树共有的节点(如 If

trait Hierarchy {
  sealed trait Expr
}
trait If {
  this: Hierarchy =>
  case class If(cond: Expr, yes: Expr, no: Expr) extends Expr
}
trait Word {
  this: Hierarchy =>
  case class Word(name: String) extends Expr
}

object SimpleExpr extends Hierarchy with If with Word
//object OtherExpr extends Hierarchy with If with Integer

object Demo extends App {
  import SimpleExpr._
  def func(expr: Expr) = expr match {
    case If(cond, yes, no) => cond
    // compiler should emit warning
  }
}

因为 sealed 不是可传递的,所以我不清楚缺少编译错误是否是错误。

我注意到向 match 表达式添加另一个 case 会导致编译器发出 "unreachable code" 警告。这是我修改后的代码版本:

#!/usr/bin/env scala
Demo.main(args)

sealed trait Hierarchy {
  sealed trait Expr
}
trait If {
  this: Hierarchy =>
  case class If(cond: Expr, yes: Expr, no: Expr) extends Expr
}
trait Word {
  this: Hierarchy =>
  case class Word(name: String) extends Expr
}

object SimpleExpr extends Hierarchy with If with Word
//object OtherExpr extends Hierarchy with If with Integer

object Demo extends App {
  import SimpleExpr._
  def func(expr: Expr) = expr match {
    case If(cond, yes, no) => cond
    // compiler should emit warning
    case Word(name) => printf("word[%s]\n",name)
  }
  func(Word("yo!"))
}

这是我 运行 它得到的结果:

warning: unreachable code
case Word(name) => printf("word[%s]\n",name)
one warning found
word[yo!]

警告不正确,正在执行unreachable代码。

case Word 行被注释掉时,这是我得到的:

scala.MatchError: Word(yo!) (of class Main$$anon$Word$Word)
    at Main$$anon$Demo$.func(demo.sc:21)

但是,以下确实会发出所需的警告:

#!/usr/bin/env scala
Demo.main(args)

sealed trait Expr
case class Word(name: String) extends Expr
case class If(cond: Expr, yes: Expr, no: Expr) extends Expr

trait Hierarchy
trait IfExpr {
  this: Hierarchy =>
}
trait WordExpr {
  this: Hierarchy =>
}

object SimpleExpr extends Hierarchy with IfExpr with WordExpr
//object OtherExpr extends Hierarchy with If with Integer

object Demo extends App {
  import SimpleExpr._
  def func(expr: Expr) = expr match {
    case If(cond, yes, no) => cond
    // compiler should emit warning
    // case Word(name) => printf("word[%s]\n",name)
  }
  // func(Word("yo!"))
}

这是我收到的警告:

demo.sc:22: warning: match may not be exhaustive.
It would fail on the following input: Word(_)
  def func(expr: Expr) = expr match {
                     ^

我终于找到了达到预期效果的解决方案:

trait Hierarchy {
  sealed trait Expr
  case class Unit() extends Expr
}
trait AddIf extends Hierarchy {
  case class If(cond: Expr) extends Expr
}
trait AddWord extends Hierarchy {
  case class Word(name: String) extends Expr
}
trait AddSymb extends Hierarchy {
  case class Symb(name: String) extends Expr
}


object AST1 extends Hierarchy with AddIf
object AST2 extends Hierarchy with AddSymb with AddIf


object TestMatch extends App {
  def match1(e: AST1.Expr) = e match {
    case AST1.If(cond) => 1
  }
  def match2(e: AST2.Expr) = e match {
    case AST2.If(cond) => 1
    case AST2.Unit() => 1
  }
}