How create new Scala Map class? -
in scala want create generic new map
class called myclass
. in class want maintain generality of types , modify method ++ sequence type. ++ method must merge equal object same key in map.
example
val map1 = ("a"->seq(1,2)) val map2 = ("a"->seq(2,3))
the result must
map1++map2 = ("a"->seq(1,2,3))
and not
map1++map2 = ("a"->seq(2,3))
for other type mymap
must same of "classic" map class.
as others have pointed out in comments, should try first , come if encounter specific problem. here's how can start, given want implement map trait:
class myclass[a, +b] extends map[a, b] { def get(key: a): option[b] = ??? def iterator: iterator[(a, b)] = ??? def +[b1 >: b](kv: scala.tuple2[a, b1]): map[a, b1] = ??? def -(key: a): map[a, b] = ??? override def ++[b1 >: b](xs: scala.collection.gentraversableonce[scala.tuple2[a, b1]]) = ??? }
you can start existing map implementation, such as
class myclass[a, +b] extends scala.collection.immutable.hashmap[a, b] { override def ++[b1 >: b](xs: scala.collection.gentraversableonce[scala.tuple2[a, b1]]) = ??? }
however, in case compiler warn inheritance existing implementation unwise because of implementation details.
Comments
Post a Comment