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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:33:48+00:00 2026-06-10T05:33:48+00:00

I think I’d like to be able to do something like the following (clearly

  • 0

I think I’d like to be able to do something like the following (clearly garbage) code illustrates:

// Clearly nonsensical
case class Example(a: String) {
    def a: Array[Byte] = a.getBytes
}

The gist of it is that I want to write an accessor method for a case class that is named identically to one of its constructor arguments.

I’m using a JSON serialization library called Jerkson that, according to my understanding, will behave in the way I want it to if I define a class in this manner. I’m basing that assumption on this code. Currently, I’m stumped.

If this isn’t possible, could anyone offer some insight on what the Jerkson library code is attempting to do?

  • 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-10T05:33:50+00:00Added an answer on June 10, 2026 at 5:33 am

    Scala automatically creates a method with the same name as any val declared in a class (including the fields of case classes) to support a concept called referential transparency. This is also why you can override a def with a val. If you’re still skeptical, you can test it yourself like this:

    First, create a Scala file with a single case class.

    // MyCase.scala
    case class MyCase(myField1: Int, myField2: String)
    

    Now, compile the file with scalac. This should result in two classes. For the example above I get MyCase.class (representing the actual case class type) and MyCase$.class (representing the auto-generated companion object for the case class).

    $ scalac MyCase.scala 
    $ ls
    MyCase$.class MyCase.class  MyCase.scala
    

    Now you can examine the resulting .class file corresponding to the case class you declared using javap. (javap standard tool for examining Java bytecode—it’s distributed along with javac in the JDK.)

    $ javap -private MyCase
    Compiled from "MyCase.scala"
    public class MyCase extends java.lang.Object implements scala.Product,scala.Serializable{
        private final int myField1;
        private final java.lang.String myField2;
        public static final scala.Function1 tupled();
        public static final scala.Function1 curry();
        public static final scala.Function1 curried();
        public scala.collection.Iterator productIterator();
        public scala.collection.Iterator productElements();
        public int myField1();
        public java.lang.String myField2();
        public MyCase copy(int, java.lang.String);
        public java.lang.String copy$default$2();
        public int copy$default$1();
        public int hashCode();
        public java.lang.String toString();
        public boolean equals(java.lang.Object);
        public java.lang.String productPrefix();
        public int productArity();
        public java.lang.Object productElement(int);
        public boolean canEqual(java.lang.Object);
        private final boolean gd1$1(int, java.lang.String);
        public MyCase(int, java.lang.String);
    }
    

    Notice how the resulting class has both a private final int myField1 and a public int myField1() corresponding to the case class’s myField1 field. The same for myField2.

    On the JVM method return types are not part of the method signature. This means that if two methods have the same name and the same argument types then they’re considered to be conflicting method declarations. This means you can’t declare the def a: Array[Byte] in your example because val a: String already exists, also taking no arguments.

    Update:

    I just looked at the library code and according to the examples the case classes should just work. There is a note in the README saying that parsing case classes does not work in the REPL. Could that be your problem? If not, you should really post the error you’re getting. Edit: Never mind, I see the error you’re talking about in your link to your other post. If I think of a response to that problem I’ll post it over there.

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

Sidebar

Related Questions

Think about the following: Your ISP offers you a dynamic ip-address (for example 123.123.123.123).
Think of the following code: static int Main() { byte[] data = File.ReadAllBytes(anyfile); SomeMethod(data);
Think of a equations system like the following: a* = b + f +
Think I have an integer array like this: a[0]=60; a[1]=321; a[2]=5; now I want
Think about the games like Spiral Knights and Minecraft(Which I believe both use LWJGL)
I think it will be very hard for you to read the code but
Think of the following services on one box: SOCKS proxy HTTP proxy SSH service
I think I'm missing something basic here. Why is the third IF condition true?
Think about a speedometer, and imagine all these little strokes around it like 10
Think of these lines of code : @boss_locations = BossLocation.order('min_level asc').all @discovered = current_user.discovered_locations.includes(:boss_location).all

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.