Search code examples
scalaexistential-type

Where is the scaladoc for scala.language.existentials?


When I tried to write a class that accepts any Class[_] as a parameter:

case class A(klass: Class[_])

I got this error:

test.scala:1: warning: inferred existential type Option[Class[_$1]] forSome { type $1 }, which cannot be expressed by wildcards, should be enabled by making the implicit value scala.language.existentials visible. This can be achieved by adding the import clause 'import scala.language.existentials' or by setting the compiler option -language:existentials. See the Scala docs for value scala.language.existentials for a discussion why the feature should be explicitly enabled. case class A(klass: Class[]) ^ one warning found

I'm willing to know why it doesn't work. But where is "the Scala docs for value scala.language.existentials"? I googled "scaladoc scala.language.existentials" but got some threads I can't understand.

Clarification: I know the 'correct' way to implement such a class is:

case class A[T](klass: Class[T])

But I want to know what the warning message means.


Solution

  • In this case what you're looking for is a ClassTag:

    class A[T](implicit val tag : reflect.ClassTag[T])
    

    This gives you the ClassTag object for the generic parameter that you can use to access the Class given when objects are being created.

    As for existentials, you can check: