Make Compile Fail on Non-Exhaustive Match in SBT

喜你入骨 提交于 2019-12-05 12:38:41

问题


Let's say that I have a trait, Parent, with one child, Child.

scala> sealed trait Parent
defined trait Parent

scala> case object Boy extends Parent
defined module Boy

I write a function that pattern matches on the sealed trait. My f function is total since there's only a single Parent instance.

scala> def f(p: Parent): Boolean = p match { 
     |   case Boy => true
     | }
f: (p: Parent)Boolean

Then, 2 months later, I decide to add a Girl child of Parent.

scala> case object Girl extends Parent
defined module Girl

And then re-write the f method since we're using REPL.

scala> def f(p: Parent): Boolean = p match { 
     |   case Boy => true
     | }
<console>:10: warning: match may not be exhaustive.
It would fail on the following input: Girl
       def f(p: Parent): Boolean = p match { 
                                   ^
f: (p: Parent)Boolean

If I were to encounter a non-exhaustive match, then I'd get a compile-time warning (as we see here).

However, how can I make the compilation fail on a non-exhaustive match?


回答1:


You can add -Xfatal-warnings to Scalac's options. That way any warning will be treated as an error.

In sbt, you can achieve that with:

scalacOptions += "-Xfatal-warnings"



回答2:


Perhaps you could put in a default case to catch post-defined elements and bind it to a partial function which you can manage separately. Then the partial will act as a "default handler".

  sealed trait Animal
  case class Dog(name: String) extends Animal
  case class Cat(name: String) extends Animal

  val t: Animal = Dog("fido")

  // updates when the trait updates
  val partial = PartialFunction[Animal, Unit] {
    case Dog(_) => println("default dog")
    case Cat(_) => println("default cat")
    case _ => throw new RuntimeException("Broken")
  }

  // matches that may be out of date refer to it
  t match {
    case Dog(_) => println("specific dog")
    case t => partial(t)
  }

Or perhaps you can just use PartialFunctions all the way through and chain them together.



来源:https://stackoverflow.com/questions/28327383/make-compile-fail-on-non-exhaustive-match-in-sbt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!