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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:40:58+00:00 2026-05-31T23:40:58+00:00

I have a class which gets serialized and deserialized in a session, and I

  • 0

I have a class which gets serialized and deserialized in a session, and I need to perform pattern matching on inner classes. I am having problems with the identity of the inner classes:

class Tree(val id: Int) {
  override def hashCode = id
  override def equals(that: Any) = that.isInstanceOf[Tree] &&
    that.asInstanceOf[Tree].id == id

  case object EmptyValue
}

val t1 = new Tree(33)
val t2 = new Tree(33)

t1 == t2 // ok 
t1.EmptyValue == t2.EmptyValue // wrong -- reports 'false'

What is an elegant way to fix the identity for EmptyValue to not ‘escape path dependency’ so to speak. I have code like the following which breaks when serialization is taking place:

def insert(point: Point, leaf: Leaf): Unit = {
  val qidx = hyperCube.indexOf(point)
  child(qidx) match {
    case EmptyValue => ...
    ...
  }
}

That is, although the compiler says my match is exhaustive, I get a runtime MatchError when using serialization (I am having custom code that writes to/reads from byte arrays). For example, I am calling from within the tree having Tree$EmptyValue$@4636 and retrieve a Tree$EmptyValue$@3601 and they don’t match.

EDIT

I have conducted further tests, because I would really like to avoid having to move the inner types out, as they will need to have type parameters and thus break all pattern matching code. It looks as if the problem only occurs with that particular case object:

class Tree {
  sealed trait LeftChild
  case object  EmptyValue   extends LeftChild
  sealed trait LeftNonEmpty extends LeftChild
  final case class LeftChildBranch() extends LeftNonEmpty

  def testMatch1(l: LeftChild) = l match {
    case EmptyValue        => "empty"
    case LeftChildBranch() => "non-empty"
  }

  def testMatch2(l: LeftChild) = l match {
    case EmptyValue      => "empty"
    case n: LeftNonEmpty => "non-empty"
  }
}

val t1 = new Tree
val t2 = new Tree

t1.testMatch1(t2.LeftChildBranch().asInstanceOf[t1.LeftChild])       // works!!!
t1.testMatch2(t2.LeftChildBranch().asInstanceOf[t1.LeftChild])       // works!!!
t1.testMatch1(t2.EmptyValue.asInstanceOf       [t1.EmptyValue.type]) // fails
  • 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-31T23:40:59+00:00Added an answer on May 31, 2026 at 11:40 pm

    Existential type could express this.

    scala> case class Tree(id: Int) {
         |    case object EmptyValue {
         |       override def hashCode = 0
         |       override def equals(that: Any) = that.isInstanceOf[x.EmptyValue.type forSome { val x: Tree }]
         |    }
         | }
    defined class Tree
    
    scala> val t1 = Tree(33)
    t1: Tree = Tree(33)
    
    scala> val t2 = Tree(33)
    t2: Tree = Tree(33)
    
    scala> t1.EmptyValue == t2.EmptyValue
    res0: Boolean = true
    

    Bytecode for equals:

    scala> :javap -v Tree.EmptyValue
    
    ...
    public boolean equals(java.lang.Object);
      Code:
       Stack=1, Locals=2, Args_size=2
       0:   aload_1
       1:   instanceof  #23; //class Tree$EmptyValue$
       4:   ireturn
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In our project, we have a class KnowledgeBaseManager, which gets used by other classes
I have java class with a method which gets an image from a website:
I have a problem that, I have a serializable class which sets and gets
I have a class which I'm serialising to send over a unix socket and
I have a class which implements UserControl. In .NET 2005, a Dispose method is
I have a class which is marked with a custom attribute, like this: public
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a class which looks something like this: public class Test { private
I have a class which constructor takes a Jakarta enums . I'm trying to
I have a class which inherits from QTreeWidgetItem and I intercept the click event.

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.