Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 238
Lai Yu-Hsuan Avatar asked Dec 20 '13 19:12

Lai Yu-Hsuan


1 Answers

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:

  • Existential types in Scala by Miles Sabin
  • Haskell documentation about them
  • Scala types of types
like image 100
Maurício Linhares Avatar answered Sep 21 '22 01:09

Maurício Linhares