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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:59:28+00:00 2026-05-28T06:59:28+00:00

I have just noticed that whilst I need clarifying parens when adding a pair

  • 0

I have just noticed that whilst I need clarifying parens when adding a pair to a map, I don’t need them when doing a re-assignment:

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Server VM, Java 1.6.0_18).
Type in expressions to have them evaluated.
Type :help for more information.

Get me some values

scala> var n = Map.empty[Int, String]
n: scala.collection.immutable.Map[Int,String] = Map()

Trying to add to the map with no clarifying parentheses:

scala> n + 1 -> ""
<console>:30: error: type mismatch;
 found   : Int(1)
 required: (Int, ?)
              n + 1 -> ""
                  ^

Fails as I expected it to. But doing the same via a re-assignment does not:

scala> n += 1 -> ""

scala> n
res12: scala.collection.immutable.Map[Int,String] = Map(1 -> "")

What is going on? Why is this not failing? Is scalac adding parens itself?

  • 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-28T06:59:29+00:00Added an answer on May 28, 2026 at 6:59 am

    It’s just a precedence issue. + and - have the same precedence. = is lower. So Scala sees (n + 1) -> "" in the former case, and n += (1 -> "") in the latter (which is then transformed to n = (n + (1 -> "")) according to the normal rules for assignment operators.

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

Sidebar

Related Questions

Just noticed that Opengl ES 1.0, 1.1, and probably also 2.0 don't have an
I have just noticed that a multidimensional array in C# does not implement IEnumerable<T>
I have just noticed that my ViewController does not call init (See below) when
I just noticed that if I have an identity column in a table, when
I'm using protobuf-net r278 in C#, and I just noticed that if I have
I've noticed that just in the last year or so, many major websites have
I have noticed that the Windows Vista/7 dialogs look MUCH better than just a
I just started using Qt and noticed that all the example class definitions have
I'm using Visual Studio 2008 and I have just noticed that the debugger is
I have just uploaded my first app to iTunes Connect and noticed that my

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.