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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:43:17+00:00 2026-06-13T15:43:17+00:00

I am writing a class which extends Scala’s immutable map with some custom constructor

  • 0

I am writing a class which extends Scala’s immutable map with some custom constructor logic. As a simple example, say I want a map of integers to strings that is initialized as 1 -> "one", 2 -> "two". In the REPL I can write the following.

scala> import collection.immutable.HashMap
import collection.immutable.HashMap

scala> HashMap[Int, String](1->"one", 2->"two")
res0: scala.collection.immutable.HashMap[Int,String] = Map(1 -> one, 2 -> two)

In my program I’d like to use the same constructor call, but I get a “too many arguments for constructor” error when I try to put it in the class definition line.

scala> class MyMap extends HashMap[Int, String](1->"1", 2->"2")
<console>:8: error: too many arguments for constructor HashMap: ()scala.collection.immutable.HashMap[Int,String]
   class MyMap extends HashMap[Int, String](1->"1", 2->"2")
               ^

Given that the way to call superclass constructors is in the class definition, I figured that any expression that creates a HashMap in the REPL should also work in the definition, but there’s some subtlety I’m missing here.

(I think extending the concrete class HashMap instead of a trait is the right thing to do here because I want to use the default map implementation. Extending HashMap is what I’d do in Java, but I’m not 100% certain that extending concrete collection classes is the most Scalaesque way to operate.)

Because I want MyMap to be immutable, I need to specify the initial values at constructor time. I can trying doing the initialization inside the apply function of the companion object like so:

class MyMap extends HashMap[Int, String]

object MyMap {
  def apply() = new MyMap ++ List(1 -> "one", 2 -> "two")
}

But MyMap() returns an immutable map instead of a MyMap.

What is the correct way to initialize MyMap?


This link about implementing Map with concrete types is relevant.


  • 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-13T15:43:18+00:00Added an answer on June 13, 2026 at 3:43 pm

    You get an error here because when you write Map() you don’t call the constructor of Map. Instead you call the apply method of it’s companion object (or more precise the apply method of one of its superclasses. See om-nom-noms comment):

    scala> Map(1 -> "one")
    res0: scala.collection.immutable.Map[Int,String] = Map(1 -> one)
    
    scala> Map.apply(1 -> "one")
    res1: scala.collection.immutable.Map[Int,String] = Map(1 -> one)
    

    If you want to have an own Map implementation you need to create your own implementation. The easiest thing I came up with is:

    object MyMap {
      def apply(ts: (Int, String)*): MyMap[Int, String] = new MyMap(ts.toMap)
      def apply(): MyMap[Int, String] = apply(1 -> "one", 2 -> "two")
    }
    
    class MyMap[A, B] private(t: Map[Int, B]) extends Map[Int, B]  {
      private val internalMap = t
      def +[B1 >: B](kv: (Int, B1)) = new MyMap(internalMap + kv)
      def -(key: Int) = new MyMap(internalMap - key)
      def get(key: Int) = internalMap.get(key)
      def iterator = internalMap.iterator
    }
    
    scala> MyMap()
    res1: MyMap[Int,String] = Map(1 -> one, 2 -> two)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a custom flex preloader which extends the IPreloaderDisplay class. How can
I was writing some class, which extends QObject and has few Q_PROPERTY I`m use
All, I am writing a multithread program. The class which extends Thread has a
Lets say class C and D extend class B which extends class A I
So I'm writing a class that extends a dictionary which right now uses a
I'm writing some Scala code which uses the Apache POI API. I would like
In the application I am writing, I have a main class which extends the
I'm writing a class which extends Fragment and I would like to use it
I've created a custom check-in policy for TFS by writing a class that extends
I am currently writing a Dialog class (lookless control) which extends Window, and am

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.