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

The Archive Base Latest Questions

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

What is the reason for val s not (?) being automatically final in singleton

  • 0

What is the reason for vals not (?) being automatically final in singleton objects? E.g.

object NonFinal {
   val a = 0
   val b = 1

   def test(i: Int) = (i: @annotation.switch) match {
      case `a` => true
      case `b` => false
   }
}

results in:

<console>:12: error: could not emit switch for @switch annotated match
          def test(i: Int) = (i: @annotation.switch) match {
                                                     ^

Whereas

object Final {
   final val a = 0
   final val b = 1

   def test(i: Int) = (i: @annotation.switch) match {
      case `a` => true
      case `b` => false
   }
}

Compiles without warnings, so presumably generates the faster pattern matching table.

Having to add final seems pure annoying noise to me. Isn’t an object final per se, and thus also its members?

  • 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-10T23:09:50+00:00Added an answer on June 10, 2026 at 11:09 pm

    This is addressed explicitly in the specification, and they are automatically final:

    Members of final classes or objects are implicitly also final, so
    the final modifier is generally redundant for them, too. Note, however, that
    constant value definitions (§4.1) do require an explicit final modifier, even if
    they are defined in a final class or object.

    Your final-less example compiles without errors (or warnings) with 2.10-M7, so I’d assume that there’s a problem with the @switch checking in earlier versions, and that the members are in fact final.


    Update: Actually this is more curious than I expected—if we compile the following with either 2.9.2 or 2.10-M7:

    object NonFinal {
      val a = 0
    }
    
    object Final {
      final val a = 0
    }
    

    javap does show a difference:

    public final class NonFinal$ implements scala.ScalaObject {
      public static final NonFinal$ MODULE$;
      public static {};
      public int a();
    }
    
    public final class Final$ implements scala.ScalaObject {
      public static final Final$ MODULE$;
      public static {};
      public final int a();
    }
    

    You see the same thing even if the right-hand side of the value definitions isn’t a constant expression.

    So I’ll leave my answer, but it’s not conclusive.

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

Sidebar

Related Questions

For some reason my values are not being stored in the array: var req
Reason I ask is because they suggest not to commit dependencies on your repos.
This seems like it should be really simple, but for some reason, I'm not
My application is freezing with a reason that I could not figure out, it
I am reading Scala and I am wondering ... Why val capacity : Int
Any reason why the following code would not fire on keypress but would on
For some reason the script below is not working. This is the code I
For some reason, the window.setTimeout(update, 5000); does not work. It does not retry the
Is there some rationale for val() being useless for checkbox controls, whereas it is
for some reason only the asp.net hyperlink is not workin with jquery. Any suggestions?

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.