Entering Scala Kits

I wanted to explore the new Scala collection structure by creating a very general prefix tree. Keys and values ​​should not only be parameters, but the types of cards used in each node should also be parameters. So I tried this:

import collection.immutable.MapLike

class PrefixMap[+M[K1,+V1] <: Map[K1,V1] with MapLike[K1,V1,M[K1,V1]],K,+V](val content: Option[V], val children: M[K,PrefixMap[M,K,V]])
  extends Map[Iterable[K],V]
  with MapLike[Iterable[K],V,PrefixMap[M,K,V]] {

    override def empty: PrefixMap[M,K,V] = new PrefixMap[M,K,V](None, children.empty)
}

But this does not compile:

PrefixMap.scala:19: error: type mismatch;
 found   : scala.collection.immutable.Map[K,PrefixMap[M,K,V]]
 required: M[K,PrefixMap[M,K,V]]
    override def empty: PrefixMap[M,K,V] = new PrefixMap[M,K,V](None, children.empty)
                                                                               ^
one error found

It bothers me. From the documentation you can see that MapLike has an empty one that returns "This". So, since children are of type M [K, PrefixMap [M, K, V]], children.empty must also be of this type.

What is going wrong and can it be fixed?

+3
source share
1 answer

Well, the problem is that MapLikedetermines emptywhich returns This, but Map.emptyreturns Map!

, :

override def empty: PrefixMap[M,K,V] = new PrefixMap[M,K,V](None, (children: MapLike[K,PrefixMap[M,K,V],M[K,PrefixMap[M,K,V]]]).empty)

, Map. , , .

+3

Source: https://habr.com/ru/post/1787178/


All Articles