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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:35:40+00:00 2026-06-09T18:35:40+00:00

Given a Map[String, Int] in Scala, I want to create a trait that allows

  • 0

Given a Map[String, Int] in Scala, I want to create a trait that allows me to add extra logic to the add and get methods. What would be the correct syntax for doing this? (Note: I also want to invoke the super method in each case)

I tried something like:

var mymap: Map[String, Int] with mytrait[String, Int] = Map[String, Int]()

trait mytrait[A, B] {

    abstract override def +[ B1 >: B] (kv: (A, B1)) : Map[A, B1] = { /* ... */ }

}

But the interpreter complains so I’m obviously missing something syntactically, specifically with the use of the type parameters.

Many thanks for the help

To be more specific: What I have is a map already in place in my code, and so rather than rewrite large chunks of my program, I want to add logic to the + method and get method of the Map so that it does extra logic every time items are added to the map elsewhere in the program. Hence why I opted for a trait to add functionality to the map

Here is my code at present :

    trait RegManager[A, B] extends scala.collection.Map[A, B] {
    case class KeyAlreadyExistsException(e: String) extends Exception(e)

    abstract override def + [B1 >: B] (kv: (A, B1)): scala.collection.Map[A, B1] = {

        super.+[B1](kv)
    }

    abstract override def get(key: A): Option[B] = super.get(key)
    abstract override def iterator: Iterator[(A, B)] = super.iterator
    abstract override def -(key: A): scala.collection.Map[A, B] = super.-(key)
}



var regs = new scala.collection.Map[String, Int] with RegManager[String, Int]

Update:
In the end I opted for a wrapper style implementation (Decorator DP) which got the job done. I am only 6 months in to my Scala career so I might not have understood the capabilities of traits correctly yet?! Great language though!

  • 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-09T18:35:41+00:00Added an answer on June 9, 2026 at 6:35 pm

    either MyTrait[A,B] extends Map[A,B] or MyTrait[A,B] {this: Map[A,B] => ...


    Sorry, in fact it is unlikely you can make that work this way.

    First part of my answer was related to the fact that your declaration of MyTrait does not reference Map, so there is no chance that it could override the + of Map. You need something like

    trait MyTrait[A,B] extends Map[A,B] {
      abstract override def +[B1 >: B](kv: (A, B1)) : Map[A, B1] = {
        println("adding to map") // this is the new part
        super.+(kv) 
       }
    }
    

    However that will not get you very far, for many reasons :

    in the declaration

    var mymap: Map[String, Int] with mytrait[String, Int] = Map[String, Int]()
    

    the value Map[String, Int]() has type Map and is unrelated to your trait. Declaring the variable mymap of type Map with mytrait will not make it so. It will just prevent it to compile.

    MyTrait must appear in the creation of the map, not just in the type declaration of the variable. That would be something like new Map[A,B] with MyTrait[A,B] (note the new).

    However, Map is a trait, not a concrete class, so you have to bring in some concrete implementation. Should you manage to do that, you then have to be careful that the new map returned by + has type MyTrait mixed in too (which of course your concrete implementation is unlikely to do). Otherwise, once you have added one element, you are back to a Map without MyTrait.

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

Sidebar

Related Questions

Given std::map<int,std::string> myMap; fillMyMapWithStuff(myMap); // modify key values - I need to add a
I've got a JSON string that I want to convert to a Map structure
val m = scala.collection.mutable.Map[String, Int]() // this doesn't work m += (foo, 2) //
Given the following code: static member private getIntValue (_map:Map<string, int>) (_key:string) = if (_map.ContainsKey
My requirement is that given a string as key to the map, I should
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
I need to convert a navigable map to a 2D String array. Below given
I want to draw an area from given points on a map using the
I found the following statement: Map is an object that stores key/volume pairs. Given
Given the following code : public class Game { private Map<String,Coordinate> m_myHashMap; ... //

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.