A hashtable (int) is more specific than a hashtable (map)

I am curious why the error occurred:

scala> import collection.JavaConverters._
import collection.JavaConverters._

scala> val m = Map("one"->1)
m: scala.collection.immutable.Map[String,Int] = Map(one -> 1)

scala> val jm = m.asJava
jm: java.util.Map[String,Int] = {one=1}

scala> val hm = new java.util.Hashtable(jm)
<console>:12: error: type mismatch;
 found   : java.util.Map[String,Int]
 required: Int
       val hm = new java.util.Hashtable(jm)
                                        ^

scala> import java.util._
import java.util._

scala> val hm: Dictionary[String,Int] = new java.util.Hashtable(jm)
hm: java.util.Dictionary[String,Int] = {one=1}

The original question is here.

It is too late at night to view the congestion.

Incorrect speculation:

It must choose between constructors that accept an int or collection. There seems to be a more specific method for a polymorphic method with a more specific type of result. Perhaps Hashtable<K, V>()more specific than Hashtable<String, Integer>because it can be <K,V>more specific than <String, Integer>, but not vice versa.

No, it is not:

implicitly[Hashtable[String,Integer] <:< Hashtable[_,_]]

Actually ctor

public Hashtable(Map<? extends K, ? extends V> t)
+4
source share
1 answer

, retronym, , , .

:

package rawj;

public class C<K, V> {
    public C(C<? extends K, ? extends V> other) { }
}

:

scala> new rawj.C(null)
<console>:8: error: inferred type arguments [?1,?0] do not conform to class C type parameter bounds [K,V]
              new rawj.C(null)
              ^

scala> val c: rawj.C[Int,Int] = new rawj.C(null)  // supply type args
c: rawj.C[Int,Int] = rawj.C@483d5954

C ( ), , .

, , .

+1

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


All Articles