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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:26:51+00:00 2026-05-17T21:26:51+00:00

Considering the following Scala snippet: case class Foo(v1: String, v2: Int, v3: Any) def

  • 0

Considering the following Scala snippet:

case class Foo(v1: String, v2: Int, v3: Any)

def inspect(p: Product) =
  (0 until p.productArity).foreach(i => println(p.productElement(i)))

inspect(Foo("Moin", 77, null))

Does the invocation of inspect() here means that reflection is used (in whatever way)?

I’d like to somehow be able to access the fields of a case-class without having to explicitly refer to them, e.g. by foo.v1 and I’d favour a solution that does not require reflection since I expect that it entails some overhead.

  • 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-17T21:26:51+00:00Added an answer on May 17, 2026 at 9:26 pm

    No reflection will be used for the productElement. It’s a compiler trick. Adding case before a class doesn’t just create a companion object (with apply method and so on, see http://www.scala-lang.org/node/258), it also extends the class from the trait Product. The compiler creates implementations of the abstract methods productArity and productElement.

    The output of scalac -print Foo.scala shows it:

    ... case class Foo extends java.lang.Object with ScalaObject with Product {
    ...
    override def productArity(): Int = 3;
    override def productElement(x$1: Int): java.lang.Object = {
      <synthetic> val temp6: Int = x$1;
      (temp6: Int) match {
        case 0 => {
          Foo.this.v1()
        }
        case 1 => {
          scala.Int.box(Foo.this.v2())
        }
        case 2 => {
          Foo.this.v3()
        }
        case _ => {
          throw new java.lang.IndexOutOfBoundsException(scala.Int.box(x$1).toString())
        }
      }
    };
    ...
    }
    

    If you want to access to the fields without reflection, you can use the method productElement from the trait Product

    scala> case class Foo(v1: String, v2: Int, v3: Any)
    defined class Foo
    
    scala> val bar = Foo("Moin", 77, null)
    bar: Foo = Foo(Moin,77,null)
    
    scala> bar.productElement(0)
    res4: Any = Moin
    
    scala> bar.productElement(1)
    res5: Any = 77
    
    scala> bar.productElement(2)
    res6: Any = null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Considering the following case in C#: class Foo { public Foo() { } }
Considering following code public class A { public static void main(String[] args) { new
Considering following code: class Results { public int playerId; public int score; public int
Considering the following: public class Person { public Person(string fName, string lName) { this.firstName
Considering the following database : Post -> User The entities : class User {
Considering the following struct : struct S { public string s; } What is
Considering the following string: String s = /static/201105-3805-somerandom/images/optional-folder/filename.gif; How can I remove the static/201105-3805-somerandom/
Considering the following table: create table inttest ( someint INT(10) NULL DEFAULT NULL );
Considering the following code public interface IEntity { int Id { get; set; }
Considering the following (pointless, but it's for illustration purpose) test class : public class

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.