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 6622039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:21:07+00:00 2026-05-25T21:21:07+00:00

Consider the following Scala code. val a = both a match { case both

  • 0

Consider the following Scala code.

val a = "both"

a match {
    case "both" | "foo" => println ("foo")   // case 1
    case "both" | "bar" => println ("bar")   // case 2
}

I would like match to work so that if a == "both", Scala will execute both cases. Is this possible or are there any alternatives to achieve what I want?

  • 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-05-25T21:21:08+00:00Added an answer on May 25, 2026 at 9:21 pm

    Standard pattern-matching will always match on only exactly one case. You can get close to what you want by using the fact that patterns can be treated as partial functions (see the Language Specification, Section 8.5, Pattern Matching Anonymous Functions) and by defining your own matching operator, though:

    class MatchAll[S](scrutinee : =>S) {
      def matchAll[R](patterns : PartialFunction[S,R]*) : Seq[R] = {
        val evald : S = scrutinee
        patterns.flatMap(_.lift(evald))
      }
    }
    
    implicit def anyToMatchAll[S](scrut : =>S) : MatchAll[S] = new MatchAll[S](scrut)
    
    def testAll(x : Int) : Seq[String] = x matchAll (
      { case 2 => "two" },
      { case x if x % 2 == 0 => "even" },
      { case x if x % 2 == 1 => "neither" }
    )
    
    println(testAll(42).mkString(",")) // prints 'even'
    println(testAll(2).mkString(","))  // prints 'two,even'
    println(testAll(1).mkString(","))  // prints 'neither'
    

    The syntax is slightly off the usual, but to me such a construction is still a witness to the power of Scala.

    Your example is now written as:

    // prints both 'foo' and 'bar'
    "both" matchAll (
      { case "both" | "foo" => println("foo") },
      { case "both" | "bar" => println("bar") }
    )
    

    (Edit huynhjl pointed out that he gave a frighteningly similar answer to this question.)

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

Sidebar

Related Questions

Consider the following two fragments of code: scala> def f1(x:Any) = x match {
Consider the following Scala code (e.g., in REPL) object A{def foo:Unit = {}} object
Consider the following Scala code: object MainObject { def main(args: Array[String]) { import Integer.{
Consider the following Scala case class: case class WideLoad(a: String, b: Int, c: Float,
Consider the following Scala code: package scala_java object MyScala { def setFunc(func: Int =>
Consider the following (simplified) example: abstract class Bar[T] { val f: PartialFunction[T, T] val
Consider this code: import scala.xml.{Node,HasKeyValue} def domatch(x:Node): Node = { val hasBar = new
Consider the following Scala code: abstract class A abstract class B[T <: A] class
Consider the following code, derived from the metascala project : object Units { case
Consider the following Scala code def NOTImplementedIn[T<: AnyRef](t:T):String = throw new Exception(t.getClass.getName+: Input type

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.