match may not be exhaustive

150 views
Skip to first unread message

Koen Daenen

unread,
Apr 29, 2014, 7:37:47 AM4/29/14
to scala-l...@googlegroups.com
Below 2 very similar classes, F1 and F2
You expect F1 to behave the same as F2[Int], which it does.
The output of Test results in:
A:1
B
C
A:1
B
C

My question/remark is related to the warning of the 2.10 compiler: no warning in F1 but in F2 at "t match { ... }", I got "match may not be exhaustive. It would fail on the following input: C"

Am I missing a detail or is the warning wrong here?

package test

class F1() {
  private sealed abstract class T
  private case class A(m: Int) extends T
  private case class B() extends T
  private case object C extends T

  // No warnings here
  private def foo(t: T) = t match {
    case A(m) => println("A:" + m)
    case B() => println("B")
    case C => println("C")
  }

  def test(m: Int): Unit = {
    foo(A(m))
    foo(B())
    foo(C)
  }
}

class F2[M]() {
  private sealed abstract class T
  private case class A(m: M) extends T
  private case class B() extends T
  private case object C extends T

  // match may not be exhaustive. It would fail on the following input: C
  private def foo(t: T) = t match {
    case A(m) => println("A:" + m)
    case B() => println("B")
    case C => println("C")
  }

  def test(m: M): Unit = {
    foo(A(m))
    foo(B())
    foo(C)
  }

}

object Test {
  def main(args: Array[String]): Unit = {
    new F1().test(1)
    new F2[Int]().test(1)
  }
}

Som Snytt

unread,
Apr 29, 2014, 7:57:02 AM4/29/14
to scala-l...@googlegroups.com
On 2.11, the warning goes away if you use:

case _: C.type => println("C")





--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Zaugg

unread,
Apr 29, 2014, 8:21:13 AM4/29/14
to scala-l...@googlegroups.com
On Tue, Apr 29, 2014 at 1:37 PM, Koen Daenen <koen.daenen.al...@gmail.com> wrote:
Below 2 very similar classes, F1 and F2
You expect F1 to behave the same as F2[Int], which it does.
The output of Test results in:
A:1
B
C
A:1
B
C

My question/remark is related to the warning of the 2.10 compiler: no warning in F1 but in F2 at "t match { ... }", I got "match may not be exhaustive. It would fail on the following input: C"

Am I missing a detail or is the warning wrong here?

The warning is wrong. I've pinpointed the spot where it regressed in the lead up to 2.10.2, and lodged a bug:


Thank you for reporting!

-jason

Koen Daenen

unread,
Apr 30, 2014, 2:12:47 AM4/30/14
to scala-l...@googlegroups.com

The warning is wrong. I've pinpointed the spot where it regressed in the lead up to 2.10.2, and lodged a bug:


Thank you for reporting!

-jason


Thanks for correlating it to the bug reporting.
Some extra info: I ran my test in the Eclipse bundle http://scala-ide.org/download/sdk.html, which I downloaded recently. I came with scala 2.10.4


On 2.11, the warning goes away if you use:

case _: C.type => println("C")

I tried this as well in 2.10.4; the warning stays.
Koen
Reply all
Reply to author
Forward
0 new messages