scala - match for types failed on Map -


i'm learning scala , trying write simple code.

when tried write method this:

def func(value: any) {   value match {     case i: int => println(1)     case vector: vector[any] => println(2)     case map: map[any, any] => println(3)     case _ => println(4)   } } 

i got warn:

[warn]........:31: non-variable type argument in type pattern scala.collection.immutable.map[any,any] (the underlying of map[any,any]) unchecked since eliminated erasure [warn]       case map: map[any, any] => println(3) [warn]                    ^ [warn] 1 warning found 

i wondering why using map[any, any] warn vector[any] not.

the problem map[x, y] not covariant in type parameter x (but vector[x] is).

what mean? suppose b <: a (read, b subtype of a).

then have vector[b] <: vector[a]. makes sense: if retrieve element x vector[b], b. means a subtyping relationship. (a similar argument applies other methods.)

following similar reasoning, map[x, b] <: map[x, a] x (element retrieval key , not index, essentials remain same).

however, doesn't hold map's first type parameter. assume map[b, x] <: map[a, x] x.

we following:

val x: map[b, x] = ??? x.get(b: b) // makes sense  val y: map[a, x] = x // must ok, map[b, x] <: map[a, x] y.get(a: a) // bad! x doesn't know how "get" of type `a` 

therefore, map[_, _] not map[any, any]. fix error message, use:

case map: map[_, _] => ... 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -