“Too many arguments” error in Scala superclass constructor but not in REPL

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

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:

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) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!