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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:24:01+00:00 2026-05-13T01:24:01+00:00

See: Can I specify a meaningful name for an anonymous class in C#? In

  • 0

See: Can I specify a meaningful name for an anonymous class in C#?

In C# you can write:

var e = new { ID = 5, Name= "Prashant" };
assertEquals( 5, e.ID )

But in Scala I end up writing:

var e = (5, "Prashant")
assertEquals( 5, e._1 )

Scala maintains type safety through the use of generics (as does C#), but loses the readability of the name of each field, e.g I use “_1” instead of “ID”.

Is there anything like this in Scala?

  • 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-13T01:24:02+00:00Added an answer on May 13, 2026 at 1:24 am
    object T {
      def main(args: Array[String]) {  
        val e = new { var id = 5; var name = "Prashant" }
        assert(e.id == 5)
      }
    }
    

    Ok, let’s make stuff clear. This does use reflection on Scala 2.7 and Scala 2.8, because the type of e is, in this case, a structural type, which Scala handles through reflection. Here is the generated code, at clean-up time (scalac -Xprint:cleanup):

    package <empty> {
      final class T extends java.lang.Object with ScalaObject {
        private <synthetic> <static> var reflMethod$Cache1: java.lang.reflect.Method = null;
        private <synthetic> <static> var reflClass$Cache1: java.lang.Class = null;
        <synthetic> <static> def reflMethod$Method1(x$1: java.lang.Class): java.lang.reflect.Method = {
          if (T.this.reflMethod$Cache1.eq(null).||(T.this.reflClass$Cache1.ne(x$1)))
            {
              T.this.reflMethod$Cache1 = x$1.getMethod("id", Array[java.lang.Class]{});
              T.this.reflClass$Cache1 = x$1;
              ()
            };
          T.this.reflMethod$Cache1
        };
        @remote def $tag(): Int = scala.ScalaObject$class.$tag(T.this);
        def main(args: Array[java.lang.String]): Unit = {
          val e: java.lang.Object = {
            new T$$anon$1()
          };
          scala.this.Predef.assert(scala.Int.unbox({
            var exceptionResult1: java.lang.Object = _;
            try {
              exceptionResult1 = T.reflMethod$Method1(e.getClass()).invoke(e, Array[java.lang.Object]{})
            } catch {
              case ($1$ @ (_: java.lang.reflect.InvocationTargetException)) => {
                exceptionResult1 = throw $1$.getCause()
              }
            };
            exceptionResult1
          }.$asInstanceOf[java.lang.Integer]()).==(5))
        };
        def this(): object T = {
          T.super.this();
          ()
        }
      };
      final class T$$anon$1 extends java.lang.Object {
        private[this] var id: Int = _;
        <accessor> def id(): Int = T$$anon$1.this.id;
        <accessor> def id_=(x$1: Int): Unit = T$$anon$1.this.id = x$1;
        private[this] var name: java.lang.String = _;
        <accessor> def name(): java.lang.String = T$$anon$1.this.name;
        <accessor> def name_=(x$1: java.lang.String): Unit = T$$anon$1.this.name = x$1;
        def this(): T$$anon$1 = {
          T$$anon$1.this.id = 5;
          T$$anon$1.this.name = "Prashant";
          T$$anon$1.super.this();
          ()
        }
      }
    }
    

    There is some caching going on, but if I alternated between id and name it would invalidate the cache already. Scala 2.8 also does reflection, and also caches, but it uses a more efficient caching technique, which should provide better overall performance. For reference, here is the clean-up of Scala 2.8:

    package <empty> {
      final class T extends java.lang.Object with ScalaObject {
        final private <synthetic> <static> var reflParams$Cache1: Array[java.lang.Class] = Array[java.lang.Class]{};
        @volatile
        private <synthetic> <static> var reflPoly$Cache1: scala.runtime.MethodCache = new scala.runtime.EmptyMethodCache();
        <synthetic> <static> def reflMethod$Method1(x$1: java.lang.Class): java.lang.reflect.Method = {
          var method1: java.lang.reflect.Method = T.reflPoly$Cache1.find(x$1);
          if (method1.ne(null))
            return method1
          else
            {
              method1 = x$1.getMethod("id", T.reflParams$Cache1);
              T.reflPoly$Cache1 = T.reflPoly$Cache1.add(x$1, method1);
              return method1
            }
        };
        def main(args: Array[java.lang.String]): Unit = {
          val e: java.lang.Object = {
            new T$$anon$1()
          };
          scala.this.Predef.assert(scala.Int.unbox({
            val qual1: java.lang.Object = e;
            {
              var exceptionResult1: java.lang.Object = _;
              try {
                exceptionResult1 = T.reflMethod$Method1(qual1.getClass()).invoke(qual1, Array[java.lang.Object]{})
              } catch {
                case ($1$ @ (_: java.lang.reflect.InvocationTargetException)) => {
                  exceptionResult1 = throw $1$.getCause()
                }
              };
              exceptionResult1
            }.$asInstanceOf[java.lang.Integer]()
          }).==(5))
        };
        def this(): object T = {
          T.reflParams$Cache1 = Array[java.lang.Class]{};
          T.reflPoly$Cache1 = new scala.runtime.EmptyMethodCache();
          T.super.this();
          ()
        }
      };
      final class T$$anon$1 extends java.lang.Object {
        private[this] var id: Int = _;
        <accessor> def id(): Int = T$$anon$1.this.id;
        <accessor> def id_=(x$1: Int): Unit = T$$anon$1.this.id = x$1;
        private[this] var name: java.lang.String = _;
        <accessor> def name(): java.lang.String = T$$anon$1.this.name;
        <accessor> def name_=(x$1: java.lang.String): Unit = T$$anon$1.this.name = x$1;
        def this(): T$$anon$1 = {
          T$$anon$1.super.this();
          T$$anon$1.this.id = 5;
          T$$anon$1.this.name = "Prashant";
          ()
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see you can specify Insert, Update and Delete stored procs, but no straightforward
I can see that GD library is for images. But I can't see differences
I can see some potential difficulties with this concept but the idea is: I
I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but
I can see how you can specify Tool Locations on a Jenkins slave node
I see that i can specify this behavior in xml by using the following:
How can I specify the compatible devices for my iphone app? I can see
I see in the settings you can specify patterns to exclude files from check-ins.
If you really need to, you can specify __attribute__((weak)) in C (see scriptedmain ).
I can see it looks like an alias for an unsigned int pointer, right?

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.