缺少非详尽匹配的警告

时间:2011-11-28 03:22:09

标签: scala pattern-matching

为什么在启用-unchecked时,以下内容不会产生警告:

object Order {
   sealed trait EntryOption
   case object EmptyEntry extends EntryOption
   trait Entry extends EntryOption

   def isEmpty(a: EntryOption): Boolean = a match {
      case EmptyEntry => true
//    case _: Entry   => false
   }
}

在Scala 2.8.0的日子里,我似乎完全拥有same problem before,没有足够的答案。


修改

@Jed对我来说,仅对非抽象类Entry发出警告是没有意义的。请考虑以下情况:

trait Order {
  sealed trait EntryOption
  case object EmptyEntry extends EntryOption
  abstract sealed class Entry extends EntryOption

  def isEmpty(a: EntryOption): Boolean = a match {
    case EmptyEntry => true
 // case _: Entry   => false
  }
}

trait OrderImpl extends Order {
  final class EntryImpl extends Entry
}

出现警告的唯一方法是在Entry中设置具体的课程Order

2 个答案:

答案 0 :(得分:3)

它确实在主干上抱怨:

scala> object Order {
     |    sealed trait EntryOption
     |    case object EmptyEntry extends EntryOption
     |    trait Entry extends EntryOption
     | 
     |    def isEmpty( a: EntryOption ) : Boolean = a match {
     |       case EmptyEntry => true
     | //    case _: Entry   => false
     |    }
     | }
<console>:18: warning: match is not exhaustive!
missing combination          Entry

          def isEmpty( a: EntryOption ) : Boolean = a match {
                                                    ^
defined module Order

答案 1 :(得分:2)

条目是一种特质,而不是案例类。