Idiomatic substitution of existential types

I have a Scala code that uses existential types, which I upgrade to 2.10, and I noticed a warning about adding "import language.existentials", which makes me think that there should be a better way to write this. The code that I have comes down to:

class A { private var values = Set.empty[(Class[_], String)] def add(klass: Class[_], id: String) { val key = (klass, id) if (!values(key)) { values += key // More logic below.. } } 

I get this warning:

 [warn] test.scala:4 inferred existential type (Class[_$2], String) forSome { type _$2 }, which cannot be expressed by wildcards, should be enabled [warn] by making the implicit value language.existentials visible. [warn] This can be achieved by adding the import clause 'import language.existentials' [warn] or by setting the compiler option -language:existentials. [warn] See the Scala docs for value scala.language.existentials for a discussion [warn] why the feature should be explicitly enabled. [warn] val key = (klass, id) 

Is there a way in which I can rewrite my code, not generate this warning (or require import), or is this the most idiomatic way to express it? I never ask about a parameter of type Class anywhere in the code.

+7
source share
2 answers

A warning about the conclusion of an existential type, which is usually undesirable. Either add an import statement or make it explicit:

 val key: (Class[_], String) = (klass, id) 
+8
source

If you specify a type parameter for the add method, the warning disappears. This does not affect what can be stored in var values. I don't have a good answer why, but this is a workaround. Hopefully someone more capable will also answer with an explanation.

  class A { private var values = Set.empty[(Class[_], String)] def add[T](klass: Class[T], id: String) { val key = (klass, id) if (!values(key)) { values += key // More logic below.. } } } 
+4
source