6
def bar[T: Manifest](a: Array[T]) = Array.ofDim[T](3)

class Foo

bar(Array(new Foo))  //Array[Foo] = Array(null, null, null)

Manifests seem to exist implicitly for arbitrary types, as shown above.

Since we have a context bound, this implies that there will be some types for which there is no implicit Manifest - what are they?

0__
  • 66,707
  • 21
  • 171
  • 266
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
  • Here seems to be one such situation: http://stackoverflow.com/questions/25285865/scala-no-manifest-available-for-type-t – csvan Aug 13 '14 at 12:24

2 Answers2

3

A Manifest has to be "carried" from the point where the concrete type last appears in the source code, all the way through type parameters to the place where it is required.

But everything has a manifest.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
2

I'm not sure that your deduction is correct. I haven't seen types for which there is no manifest, but I have seen situations where the type inferencer doesn't seem able to provide one.

Specifically in nesting inference situations like this:

scala> def bar[T: Manifest](a: Array[T]) = Array.ofDim[T](3)
bar: [T](a: Array[T])(implicit evidence$1: Manifest[T])Array[T]

scala> def bar2[T](a: Array[T]) = bar(a)
<console>:8: error: No Manifest available for T.
   def bar2[T](a: Array[T]) = bar(a)
                                ^

It seems that unless the manifest is 'passed around' it isn't available at the lower level - so that we can say

scala> def bar2[T: Manifest](a: Array[T]) = bar(a)
bar2: [T](a: Array[T])(implicit evidence$1: Manifest[T])Array[T]

or

scala> def bar2[T](a: Array[T])(implicit m: Manifest[T]) = bar(a)
bar2: [T](a: Array[T])(implicit m: Manifest[T])Array[T]

However quite why this is the behaviour I don't know.

Duncan McGregor
  • 17,665
  • 12
  • 64
  • 118
  • I think with your `bar2` method we get the error because we haven't constrained the type of `T` such that a Manifest must be available... but that doesn't shed much light on the types where it actually won't be. – Luigi Plinge Nov 19 '11 at 00:31