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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:37:16+00:00 2026-06-04T19:37:16+00:00

I create an ObservableMap, and a subscriber that just prints any events it receives

  • 0

I create an ObservableMap, and a subscriber that just prints any events it receives (taken from here):

class MyMap extends HashMap[Int,Int] with ObservableMap[Int,Int]

class MySub extends Subscriber[Message[(Int,Int)] with Undoable, ObservableMap[Int, Int]] {
  def notify(pub: ObservableMap[Int, Int], evt: Message[(Int, Int)] with Undoable) { 
    println(evt)
  }
}

val map = new MyMap
map.subscribe(new MySub)

Using +=, ++=, and -= work as expected:

scala> map += 1 -> 1
Include(NoLo,(1,1))
res5: map.type = Map(1 -> 1)

scala> map ++= Map(2 -> 4, 3 -> 9)
Include(NoLo,(3,9))
Include(NoLo,(2,4))
res6: map.type = Map(3 -> 9, 1 -> 1, 2 -> 4)

scala> map -= 1
Remove(NoLo,(1,1))
res7: map.type = Map(3 -> 9, 2 -> 4)

But update doesn’t work:

scala> map(4) = 16

scala> map
res9: MyMap = Map(3 -> 9, 4 -> 16, 2 -> 4)

Why? It looks like ObservableMap overrides +=, -=, and clear. Both ++= and update appear to be implemented in terms of += (by Growable and MapLike respectably), so why does it work on one but not the other?

  • 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-04T19:37:17+00:00Added an answer on June 4, 2026 at 7:37 pm

    The mutable version of HashMap calls update which in turn calls put, which does not call += so the observable += method doesn’t get called. I’m using scala 2.9.1, but this should be the same for 2.8 on.

    From HashMap:

      override def put(key: A, value: B): Option[B] = {
        val e = findEntry(key)
        if (e == null) { addEntry(new Entry(key, value)); None }
        else { val v = e.value; e.value = value; Some(v) }
      }
    
      override def update(key: A, value: B): Unit = put(key, value)
    
      def += (kv: (A, B)): this.type = { 
        val e = findEntry(kv._1)
        if (e == null) addEntry(new Entry(kv._1, kv._2))
        else e.value = kv._2
        this
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

// Create a scanner that reads from the input stream passed to us CSLexer
Create any application in Delphi XE2 and press F1 to run help system. You'll
Create Pex test to test a DLL and hardware controlled by that DLL? I
CREATE TABLE #Report( Cell int, CellValue double) Error here DECLARE @Report TABLE ( Cell
Create Trigger: SELECT @oldVersionId = (SELECT DISTINCT VERSION_ID FROM Deleted) SELECT @newVersionId = (SELECT
Create facebook app Using a cURL to post a message from an App but
Create default iPad split view based apps from template from xcode 4, then we
Create a function that can have different argument list. something like this void s(int
CREATE MATERIALIZED VIEW ORDERS_MV BUILD IMMEDIATE REFRESH COMPLETE ON DEMAND AS SELECT * FROM
CREATE PROCEDURE p_processDataFor @accountId BEGIN for each item in (select * from Accounts where

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.