【发布时间】:2021-04-20 13:40:05
【问题描述】:
我目前有这个设置,我有一个方法和一些隐式资源,并且该方法返回一个函数,我可以稍后在我的代码中使用。
type AResource = Int
def testA(s: String)(implicit aResource: AResource): (Double) => (String, Int) = (d: Double) => {
(s + d, d.toInt * aResource)
}
implicit val ar:AResource = 3
val fA = testA("org: ")
fA(3.1415)
这将按预期打印(org: 3.1415,9)。到目前为止一切顺利
但有时我想在单行中调用此方法,这迫使我将隐式显式化。
val fA2 = testA("org2: ")(ar)(1.123)
这似乎是一个小小的不便,但问题实际上更复杂一些,因为我的方法使用了TypeTag 并将隐式 typeTag 也注入到函数中。
我正在寻找的是一种定义testA 以便返回函数实现隐含的方法。
这样(显然不行)
def testB(s: String): (Double, AResource) => (String, Int) = (d: Double)(implicit aResource: AResource) => {
(s + d, d.toInt * aResource)
}
然后我就可以跑了
testB("org2: ")(1.123)
并担心最低级别的隐含
更新:
我至少找到了这个解决方案,但它还不是 100% 完美
def testC(s: String): (Double) => (AResource) => (String, Int) = (d: Double) => { implicit aResource: AResource => {
(s + d, d.toInt * aResource)
}}
val c:(String, Int) = testC("org: ")(2.4)(ar)
它确实将隐式向下移动,但我仍然必须通过硬编码。
更新 2:
Tim 为玩具问题提供了一个很好的解决方案,但仅在定义期间隐式资源已在范围内时才有效。
当隐式从范围中删除时,定义失败
【问题讨论】:
-
请注意,在 Scala 3 中,这将得到修复,您的代码将按预期工作。
-
有趣。不过,我的代码库在 Spark 中,可能不会很快