Wonpyo Park Wonpyo Park - 2 years ago 152
Scala Question

In Scala, How can Manifest implicitly be caputured in context?

def getManifest[T : Manifest] = implicitly[Manifest[T]]

class A[T]
object A {
def apply[T] = new A[T]
def getA[T : A] = implicitly[A[T]]

val m = getManifest[Int]
//res0: Manifest[Int] = Int

val a = getA[Int]
//<console>:14: error: could not find implicit value for evidence parameter of type A[Int]

Even though there is no implicit variable in context

by calling
of type
is caputred implicitly

However my custom class A,
emits an error

Because there is no implicit variable in cotext.

What is the difference between class A and Manifest

does Scala compiler knows about Manifest and do some magic ???

like, creating some implicit variables for Manifest

Answer Source

Short answer is yes. Manifests are compiler magic where the Scala compiler will automatically conjure up implicit instances of them for types as they are needed. They're used to get around type erasure on the JVM (the problem that at runtime generic types get erased so for example a List[Int] looks the same as a List[String]).

See What is a Manifest in Scala and when do you need it? for more details.

They are also deprecated now in favor of TypeTags.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download