Skip to content

Exhaustivity checking of extractors statically known to return Some #10502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TomasMikula opened this issue Sep 7, 2017 · 1 comment · Fixed by scala/scala#9312
Closed

Exhaustivity checking of extractors statically known to return Some #10502

TomasMikula opened this issue Sep 7, 2017 · 1 comment · Fixed by scala/scala#9312

Comments

@TomasMikula
Copy link

Not a bug report, but a feature request.

It would be useful to preserve exhaustivity checking if the custom extractor is declared to return Some.

object Perhaps {
  def unapply[A](oa: Option[A]): Some[Option[A]] = Some(oa)
}

Perhaps.unapply is statically known to always succeed, because its return type is Some.

Yet

scala> Option("hello") match {
     |   case Perhaps(Some(s)) => println(s)
     | }
hello

scala> 

doesn't produce any warnings, even though the match would fail on None; and

scala> List(Option("hello")) match {
     |   case Perhaps(Some(s)) :: t => println(s)
     |   case Perhaps(None   ) :: t => ()
     |   case Nil                   => ()
     | }
<console>:13: warning: match may not be exhaustive.
It would fail on the following input: List(_)
       List(Option("hello")) match {
           ^
hello

scala> 

produces a warning, even though the match is exhaustive.

This would be useful to pattern match on data types that are not defined as ADTs (or their definition as ADT is not publicly visible), but can be converted to a pattern matchable type.

@y6x2digc
Copy link

It's more simple to add something like def unapplyTotal[A](oa: Option[A]): Option[A] = oa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants