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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:00:10+00:00 2026-05-14T22:00:10+00:00

In the book Programming in Scala from Martin Odersky there is a simple example

  • 0

In the book “Programming in Scala” from Martin Odersky there is a simple example in the first chapter:

var capital = Map("US" -> "Washington", "France" -> "Paris")
capital += ("Japan" -> "Tokyo")

The second line can also be written as

capital = capital + ("Japan" -> "Tokyo")

I am curious about the += notation. In the class Map, I didn’t found a += method. I was able to the same behaviour in an own example like

class Foo() {
    def +(value:String) = {
        println(value)
        this
    }
}

object Main {
  def main(args: Array[String]) = {
   var foo = new Foo()
   foo = foo + "bar"
   foo += "bar"
  }
}

I am questioning myself, why the += notation is possible. It doesn’t work if the method in the class Foo is called test for example. This lead me to the prefix notation. Is the + a prefix notation for the assignment sign (=)? Can somebody explain this behaviour?

  • 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-14T22:00:11+00:00Added an answer on May 14, 2026 at 10:00 pm

    If you have a symbolic method that returns the same object, then appending equals will perform the operation and assignment (as a handy shortcut for you). You may also always override the symbolic method. For example,

    scala> class Q { def ~#~(q:Q) = this }
    defined class Q
    
    scala> var q = new Q
    q: Q = Q@3d511e
    
    scala> q ~#~= q
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is taken from Programming in Scala book by Martin Odersky et
I am learning Scala from Martin Odersky's book Programming in Scala. In his Packages
I was going through the Martin Odersky's book Programming in Scala with it's section
I'm a newbie to scala. I'm trying an example from the book Programming Scala.
The following example is from the book 'Programming in Scala'. Given a class 'Rational'
In Chapter 9 of Programming In Scala, there is an example method like this:
In the book Programming In Scala, chapter 23, the author give an example like:
Warning: I'm cross-posting from #scala The book Programming in Scala states that path-dependent types
From reading parts of the Programming in Scala book, I realize that Scala can
My github repo is called Programming-iOS-4-Book-Examples, because it's the example code from my book

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.