Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing warning of non-exhaustive match

Why doesn't the following produce a warning, when -unchecked is enabled:

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
   }
}

It seems I had exactly the same problem before in the days of Scala 2.8.0, with no sufficient answer.


EDIT

@Jed It doesn't make sense for me that the warning is only emitted for a non-abstract class Entry. Consider the following situation:

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
}

The only way to make the warning appear is to have a concrete class Entry in Order!

like image 549
0__ Avatar asked Nov 28 '11 03:11

0__


2 Answers

It does complain on trunk:

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
like image 53
Daniel C. Sobral Avatar answered Oct 14 '22 23:10

Daniel C. Sobral


Entry is a trait, not a case class.

like image 2
Jed Wesley-Smith Avatar answered Oct 15 '22 00:10

Jed Wesley-Smith