2016-09-14 54 views
0

我是斯卡拉新手,我从帖子中学习“上下文绑定”。但是我发现其中许多使用ClassManifest来解释“上下文绑定”作为例子。例如,什么是Scala中的ClassManifest?

def tabulate[T](len: Int, f: Int => T)(implicit m: ClassManifest[T]) = { 
    val xs = new Array[T](len) 
    for (i <- 0 until len) xs(i) = f(i) 
    xs 
} 

我觉得很奇怪的是,隐含参数m是必需的,但在函数体中从未使用过。因此,我想知道ClassManifest是什么以及它与上下文绑定的关系。谢谢!

编辑:

我见过What is a Manifest in Scala and when do you need it?之前,但它要求ManifestClassManifest,并没有在该职位有关ClassManifest解释,所以我问这个类似(但,IMO,不重复)题。

+3

可能重复[什么是Scala中的Manifest,什么时候需要它?](http://stackoverflow.com/questions/3213510/what-is-a-manifest-in-scala-and-when-do-you-need-it ) – manojlds

+0

@manojlds对不起,我是Scala的新手,但是'Manifest'与ClassManifest'一样吗?谢谢 –

+0

请注意,两者都被弃用 – dk14

回答

2

您可以在旧版本的scala(2.10之前)here中找到关于ClassManifest的说明。请阅读所有的答案,some of them explain not only Manifest but ClassManifest too

还存在着一个名为ClassManifest较弱的形式,可从知道刚刚顶级类的类型来 构造,没有 不必知道所有的参数类型。

在斯卡拉2.11.8,2.12-M4 ClassManifest已被废弃,成为了一个alias to ClassTag

type ClassManifest[T] = ClassTag[T] 

因为2.10.0它弃用。

P.S.其实this post从你可能看斯卡拉文档是完全过时的,甚至认为边界(<%)描述有deprecated too(一个相当长的时间),所以你应该避免使用它们,以及(using this simple trick if you really need them, but implicit views are not recommended in any form