Since Scala 2.7.2 there is something called Manifest
which is a workaround for Java's type erasure. But how does Manifest
work exactly and why / when do you need to use it?
The blog post Manifests: Reified Types by Jorge Ortiz explains some of it, but it doesn't explain how to use it together with context bounds.
Also, what is ClassManifest
, what's the difference with Manifest
?
I have some code (part of a larger program, can't easily include it here) that has some warnings with regard to type erasure; I suspect I can solve these by using manifests, but I'm not sure exactly how.
The compiler knows more information about types than the JVM runtime can easily represent. A Manifest is a way for the compiler to send an inter-dimensional message to the code at runtime about the type information that was lost.
This is similar to how the Kleptonians have left encoded messages in fossil records and the "junk" DNA of humans. Due to limitations of lightspeed and gravitational resonance fields, they are unable to communicate directly. But, if you know how to tune into their signal, you can benefit in ways you cannot imagine, from deciding what to eat for lunch or which lotto number to play.
It isn't clear if a Manifest would benefit the errors you are seeing without knowing more detail.
One common use of Manifests is to have your code behave differently based on the static type of a collection. For example, what if you wanted to treat a List[String] differently from other types of a List:
A reflection-based solution to this would probably involve inspecting each element of the list.
A context bound seems most suited to using type-classes in scala, and is well explained here by Debasish Ghosh: http://debasishg.blogspot.com/2010/06/scala-implicits-type-classes-here-i.html
Context bounds can also just make the method signatures more readable. For example, the above function could be re-written using context bounds like so:
A Manifest was intended to reify generic types that get type-erased to run on the JVM (which does not support generics). However, they had some serious issues: they were too simplistic, and were unable to fully support Scala's type system. They were thus deprecated in Scala 2.10, and are replaced with
TypeTag
s (which are essentially what the Scala compiler itself uses to represent types, and therefore fully support Scala types). For more details on the difference, see:In other words
Before 2013-01-04, when Scala 2.10 was released.
Not a complete answer, but regarding the difference between
Manifest
andClassManifest
, you can find an example in the Scala 2.8Array
paper:Example:
(See this SO question for illustration)
Let's also chck out
manifest
inscala
sources (Manifest.scala
), we see:So with regards to following example code:
we can see that the
manifest
function
searches for an implicitm: Manifest[T]
which satisfies thetype parameter
you provide in our example code it wasmanifest[String]
. So when you call something like:you are checking if the current
implicit m
which you defined in your function is of typemanifest[String]
and as themanifest
is a function of typemanifest[T]
it would search for a specificmanifest[String]
and it would find if there is such an implicit.