cannot find class manifest for element type T

后端 未结 1 1687
死守一世寂寞
死守一世寂寞 2020-12-29 07:25

Was trying to compile some code from this SO question and run into this error message cannot find class manifest for element type T. Here is another snippet tha

1条回答
  •  无人及你
    2020-12-29 07:45

    To provide type information you can use a context bound

    def f[T : Manifest](a:T, b:T):Array[T] = { new Array[T](2) }
    

    or the manifest as an implicit argument:

    def f[T](a:T, b:T)(implicit manifest : Manifest[T]) : Array[T] = { new Array[T](2) }
    

    The former is syntactic sugar for the later. The manifest is needed because the type information about T is missing due to generic type errasure of the JVM.

    0 讨论(0)
提交回复
热议问题