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

  • SEARCH
  • Home
  • 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 8380087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:22:01+00:00 2026-06-09T16:22:01+00:00

I’m new to Scala, and I want to write some multi-threaded code with pattern

  • 0

I’m new to Scala, and I want to write some multi-threaded code with pattern matching, and I was wondering if I could treat the pattern-matching code as atomic.

For example:

abstract class MyPoint
case class OneDim(x : Int) extends MyPoint
case class TwoDim(x : Int, y : Int) extends MyPoint

var global_point : MyPoint = new OneDim(7)

spawn {
    Thread.sleep(scala.util.Random.nextInt(100))
    global_point = new TwoDim(3, 9)
}
Thread.sleep(scala.util.Random.nextInt(100))

match global_point {
    case TwoDim(_, _) => println("Two Dim")
    case OneDim(_) => println("One Dim")
}

Is it possible that the execution will go as the following:

  1. The main thread reaches the “match global_point” code, finds out that *global_point* is not of type TwoDim and pauses (returns to the scheduler).
  2. The spawned thread changes *global_point* to be of type TwoDim
  3. The main thread returns, finds out that the *global_point* is not of type OneDim, thinks there are no matches to *global_point* and raises a NoMatch exception.

Is this kind of execution avoided internally by Scala? If it does, then how? Does the matching take a snapshot of the object and then try to match it against the patterns? Is there a limit to the snapshot depth (the match patterns can be complex and nested)?

  • 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-09T16:22:03+00:00Added an answer on June 9, 2026 at 4:22 pm

    This is not hard evidence from the specs, but it illustrates some things the compiler does for you, and that should allow you to regard a few match blocks as atomic – but definitely not all. It will be much safer if you synchronise your code yourself, or if you go with immutable objects.

    Flat example

    If you run the following script with scala -print:

    var m: Option[String] = _
    
    m match {
      case Some(s) => "Some: " + s
      case None => "None"
    }
    

    you’ll see the desugared intermediate code created by the compiler (I’ve removed some code for brevity):

    final class Main$$anon$1 extends java.lang.Object {
      private[this] var m: Option = _;
    
      private <accessor> def m(): Option = Main$$anon$1.this.m;
    
      def this(): anonymous class Main$$anon$1 = {
        <synthetic> val temp1: Option = Main$$anon$1.this.m();
    
        if (temp1.$isInstanceOf[Some]()) {
          "Some: ".+(temp1.$asInstanceOf[Some]().x())
        else if (scala.this.None.==(temp1))
          "None"
        else
          throw new MatchError(temp1)
      }
    }
    

    The possibly shared object referenced by m gets a local alias temp1, so if m is changed in the background such that it points to another object, then the match still takes place on the old object m pointed to. Hence, the situation you described above (changing global_point to point to a TwoDim instead of to a OneDim) is not a problem.

    Nested example

    It seems to generally be the case that the compiler creates local aliases to all objects that are bound in the guard of a match case, but it does not create a deep copy!
    For the following script:

    case class X(var f: Int, var x: X)
    
    var x = new X(-1, new X(1, null))
    
    x match {
      case X(f, ix) if f >  0 || ix.f > 0  => "gt0"
      case X(f, ix) if f <= 0 || ix.f <= 0 => "lte0"
    }
    

    the compiler creates this intermediate code:

    private[this] var x: anonymous class Main$$anon$1$X = _;
    
    private <accessor> def x(): anonymous class Main$$anon$1$X = Main$$anon$1.this.x;
    
    final <synthetic> private[this] def gd2$1(x$1: Int, x$2: anonymous class Main$$anon$1$X): Boolean = x$1.>(0).||(x$2.f().>(0));
    
    final <synthetic> private[this] def gd3$1(x$1: Int, x$2: anonymous class Main$$anon$1$X): Boolean = x$1.<=(0).||(x$2.f().<=(0));
    
    def this(): anonymous class Main$$anon$1 = {
      <synthetic> val temp6: anonymous class Main$$anon$1$X = Main$$anon$1.this.x();
    
      if (temp6.ne(null)) {
        <synthetic> val temp7: Int = temp6.f();
        <synthetic> val temp8: anonymous class Main$$anon$1$X = temp6.x();
        
        if (Main$$anon$1.this.gd2$1(temp7, temp8))
          "gt0"
        else if (Main$$anon$1.this.gd3$1(temp7, temp8))
          "lte0"
        else
          throw new MatchError(temp6)
      } else
        throw new MatchError(temp6)
    }
    

    Here, the compiler creates local aliases for the object x you match on, and for its two sub-objects x.f (bound to f) and x.x (bound to ix), but not for ix.f. Hence, if the structure you match on is deeply nested and your cases depend on nested objects that you don’t bind locally, then race conditions can occur. And will, as we all know, due to Murphy’s Law.

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.