[英]Scala: Two implicit manifiests
Say I have this function in a class: 说我在一个班上有这个功能:
def serializeObject[O<:MongoModel, F<:FunnelJob](mongoObject:O):F = {
grater[F].asObject(mongoObject)
}
I will not be able to compile this because I need a manifest for O and F. But I am unable to actually use to implicit manifest calls 我将无法编译此文件,因为我需要O和F的清单。但是我无法实际用于隐式清单调用
def serializeObject[O<:MongoModel, F<:FunnelJob](mongoObject:O)(implicit mf:Manifest[O], implicit m:Manifest[F]):F = {
grater[F].asObject(mongoObject)
}
is invalid, for example. 例如,无效。 How can I have two generics with two manifests? 我怎么能有两个带有两个清单的泛型?
You only need one implicit
keyword, it applies to the entire parameter list in this context: 您只需要一个implicit
关键字,它在此上下文中适用于整个参数列表:
def serializeObject[O<:MongoModel, F<:FunnelJob](mongoObject:O)(implicit mf:Manifest[O], m:Manifest[F]):F
Side notes: 旁注:
you probably don't need Manifest[O]
; 您可能不需要Manifest[O]
;
when calling, you'll need to provide the type parameters explicitly or have a clear expected type, otherwise F
will be inferred to Nothing
; 调用时,您需要显式提供类型参数或具有明确的预期类型,否则F
将被推断为Nothing
;
Manifest
s are semi-deprecated and should be replaced by ClassTag
or TypeTag
in most cases. Manifest
是不推荐使用的,在大多数情况下应由ClassTag
或TypeTag
代替。 See http://docs.scala-lang.org/overviews/reflection/typetags-manifests.html . 参见http://docs.scala-lang.org/overviews/reflection/typetags-manifests.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.