Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9237965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:39:03+00:00 2026-06-18T07:39:03+00:00

Exhaustive pattern matching is great, but it only appears to work on the left-hand

  • 0

Exhaustive pattern matching is great, but it only appears to work on the left-hand side of the case (=>) operator.

I am curious if there is a way that a person can verify that the output of a function (or expression) can be bound to that enumeration. The goal would be to have the compiler tell me when I forget to output an item from the enumeration.

In my example, I make use of the following enumeration containing three items:

object MyEnum { 
    sealed trait t 
    case object E1 extends t
    case object E2 extends t
    case object E3 extends t
}

And here is a pattern-match expression that would produce a compile-time warning (as we already know):

def foo( e : MyEnum.t ) : Boolean =
    e match {
        case MyEnum.E1 => true
        case MyEnum.E2 => false
        case MyEnum.E3 => true   // if we leave this line out, we get a warning
    }

Scala would complain if we left MyEnum.E3 out of the pattern matching expression, citing a non-exhaustive pattern match. This is profoundly beneficial, but I wonder if the reverse is possible.

Can we account for all cases of MyEnum.t on the right-hand side of =>?

Here is an example that highlights this:

def bar( s : String ) : Option[MyEnum.t] = 
    s match {
        case "a" => Some(MyEnum.E1)
        case "b" => Some(MyEnum.E2)
        case "c" => Some(MyEnum.E3)  // if we leave this out, no warning
        case _ => None
    }

In this example, if we leave out the line with MyEnum.E3, then the compiler just carries on as if nothing is wrong. The assertion I would like to make, is:

forall MyEnum.t (aliased as e) there exists Some(e) | None

I understand that this could be easily covered by a run-time test, but I’m curious if there is a way to check this statically.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:39:04+00:00Added an answer on June 18, 2026 at 7:39 am

    I am going to push my luck and claim it is not possible (let’s keep this a secret from Odersky). If the Scala compiler was able to detect such situations, I think it would be even slower than it already is 😉

    The only way I could see is to define foo similarly to how you have done, and define bar by making it iterate over foo to make a reverse map, but that doesn’t make much sense to me, and might not work in your specific case.

    I think your case is a very good example for when unit tests are useful, so why not just write one?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There appears to be many alternative Emacs auto completion mechanisms. Without an exhaustive search,
Can I disable the non-exhaustive pattern matches warning only for lambdas? I like the
Is there an exhaustive list of MySQL Time Zones? It seems that the valid
I am trying to track down a non-exhaustive pattern in a libraries code. Specifically
i want to implement the following exhaustive match, but i can't figure out to
Is it generally considered a bad practice to use non-exhaustive pattern machings in functional
Is it recommended to always have exhaustive pattern matches in Haskell, even for impossible
Is there an exhaustive list of all of the base (not used in an
I've been looking for an exhaustive list, but can't find one anywhere. To avoid
I've searched for this on different places but I haven't found an exhaustive answer.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.