简体   繁体   English

如何在闭包上声明清单

[英]How to declare a manifest on a closure

Is it possible to declare a manifest on a closure 是否可以在闭包上声明清单

instead of 代替

def extract[A](v:JValue)(implicit m: Manifest[A]) : A = v.extract[A]

something like ? 就像是 ?

def extract[A] = (v: JValue) => v.extract[A]

this declares the manifest on the method which returns the function returning you a function for a specific type: 这将在方法上声明清单,该清单将返回函数,并为您返回特定类型的函数:

def extract[A](implicit m: Manifest[A]) = (v: JValue) => v.extract[A]

other than that it is impossible for two reasons: 除此之外,由于两个原因,这是不可能的:

  1. scala functions unlike methods don't support parametric polymorphism - there is nothing like 与方法不同的Scala函数不支持参数多态性-像

    [A](v: JValue) => (m: Manifest[A]) => v.extract[A]

  2. scala functions unlike methods don't support implicit parameters. 与方法不同,scala函数不支持隐式参数。 while this is accepted syntax: 虽然这是公认的语法:

    { implicit manifest: Manifest[String] => v.extract[String] }

    the implicit here does have a different meaning: it makes the value manifest available for implicit resolution within the body of the function but has no effect when calling the function (you will have to provide the manifest explicitly) 这里的隐式确实具有不同的含义:它使值manifest可用于函数体内的隐式解析,但在调用函数时无效(您必须显式提供清单)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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