When the manifest is unavailable

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 context-related, this means that there will be some types for which there is no implicit manifest - what is it?

+6
source share
2 answers

The manifest should be โ€œtransferredโ€ from the place where the specific type of the latter appears in the source code, up to the type parameters in the place where it is necessary.

But everything has a manifest.

+3
source

I'm not sure your conclusion is correct. I have not seen types for which there is no manifest, but I have seen situations where the inferencer type does not seem to be able to provide it.

In particular, when embedding such output situations:

 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 if the manifest did not โ€œpassโ€, it is not available at a lower level, so 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, why I do not know such behavior.

+2
source

Source: https://habr.com/ru/post/901862/


All Articles