简体   繁体   English

Scala:两个隐式的ManManets

[英]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: 旁注:

  1. you probably don't need Manifest[O] ; 您可能不需要Manifest[O]

  2. 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

  3. Manifest s are semi-deprecated and should be replaced by ClassTag or TypeTag in most cases. Manifest是不推荐使用的,在大多数情况下应由ClassTagTypeTag代替。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM