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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:57:22+00:00 2026-06-13T21:57:22+00:00

Assuming defintion: case class IntegerWrapper(i : Int) and being in a situation that potentially

  • 0

Assuming defintion:

case class IntegerWrapper(i : Int)

and being in a situation that potentially large amounts of IntegerWrapper instances with i=[0..N> may be created what does one have to do to:

  1. Map this range to a fixed set of singletons [IntegerWrapper(0) .. IntegerWrapper(N)>

  2. Preserve existing value semantics for class IntegerWrapper (matching, equals, hashcode, serialization)

I am looking to do instance sharing akin to what java.lang.Integer does. I guess my question is if this can be done without having to do everything myself. Simply defining a companion object with an apply(i : Int) does not compile. Any suggestions?

  • 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-13T21:57:23+00:00Added an answer on June 13, 2026 at 9:57 pm

    Something like this?

    sealed trait IntegerWrapper {
      def i: Int
    }
    
    object IntegerWrapper extends (Int => IntegerWrapper) {
      private case class IntegerWrapperImpl(i: Int) extends IntegerWrapper
    
      private val instances: List[IntegerWrapperImpl] = ...
        /* Wrapper instances for i in [0..N) */
    
      def apply(i: Int): IntegerWrapper = instances(i)
    
      def unapply(iw: IntegerWrapper): Option[Int] = Some(iw.i)
    }
    

    The advantage is that equals and hashCode are still generated by the compiler, since IntegerWrapperImpl is a case class. The disadvantage is that you cannot directly use other compiler-added case class goodies, e.g., copy. If you want to use that, either expose IntegerWrapperImpl to the client, or, IMHO better, add copy to the IntegerWrapper interface.

    Pattern matching works as usual:

    val iw0 = IntegerWrapper(0)
    val iw1: IntegerWrapper = IntegerWrapper(1)
    
    iw0 match {
      case IntegerWrapper(0) => println("IW(0)")
      case _ => println("something else")
    } // IW(0)
    
    iw1 match {
      case IntegerWrapper(1) => println("IW(1)")
      case _ => println("something else")
    } // IW(1)
    
    iw1 match {
      case IntegerWrapper(2) => println("IW(2)")
      case _ => println("something else")
    } // something else
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming that I have this: enum { A = 0x2E, B = 0x23, C
Assuming I have a ASP.NET MVC 3 application that runs in a web farm
Assuming that parsing the equation would not be a problem, how can I make
Assuming that the larger a database gets, the longer it will take to SELECT
I have the following record definition: -record(contact, {name, email}) Assuming that I have a
Assuming the following view model definition: public class MyObject { public string Name {
Assuming you have the following database table: create table Names ( Id INT IDENTITY
Assuming I have this struct definition in C#: public struct TimeSlotInfo { public int
Using .Net framework 4. I am assuming this log4net version that the PayPal library
Assuming I have a numerical string: var foo = 0; Assume I want to

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.