I’m learning Scala’s concept of manifests and I have a basic understanding how to use it in some simple cases. What puzzles me is what’s OptNanifest and NoManifest for? I’ve never seen then used. Can someone give an example where they’re are needed/useful?
(I see that Scala 2.10 replaces the concept of Manifests with TypeTags but until 2.10 is final we have to use Manifests.)
Suppose we have the following case class and type alias:
We can now (not very usefully) make a list of things of type
F:And we can turn this into an array:
So clearly the compiler is able to find the manifest it needs as an implicit argument to the
toArraymethod onList. But if we ask for a plain oldManifestforF, we get an error:So it’s clear that the compiler is having trouble using manifests to represent the wildcard in our type alias.
The reason
toArrayworks is that it expects aClassManifest, not just aManifest. And in fact we can get aClassManifestforFwith no problems, precisely becauseClassManifestusesOptManifestto represent its type arguments—unlikeManifest, whose type arguments are just other things of typeManifest.That
<?>is the string representation ofNoManifest. It plays the role ofNonehere, allowing the compiler to represent the class information about the typeF(which is all we need for creating an array, fortunately), without saying anything about the type arguments ofFbeyond “no, I can’t model that”.