条件隐式定义Scala [英] Conditional Implicit Definitions Scala
本文介绍了条件隐式定义Scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须解决这个问题,但是我找不到正确的答案.
I have to solve this quinz but i can't find the correct answer.
trait Physics {
implicit def air: Gaz,
implicit def condense(implicit gaz: Gaz): Liquid,
implicit def freeze(implicit liquid: Liquid): Solid
implicitly[Solid]
}
您可以使用显式编写的推断参数重写最后一行吗?
Can you rewrite the last line with the inferred parameter explicitly written?
提示:应该看起来像 隐式[固体](...
Hint: It should look like implicitly[Solid](...
非常感谢您!
推荐答案
这里是一个提示:首先考虑implicitly
只是一种与其他方法一样的方法
Here is a hint: first consider implicitly
is just a method like any other
def implicitly[T](implicit e: T): T = e
让我们删除关键字implicit
这样
def implicitly[T](e: T): T = e
给出implicitly
只是一个带有参数的方法,请考虑如何使编译器满意并让方法implicitly
返回Solid
?
Given implicitly
is just a method taking arguments, think about what would you have to do to make compiler happy and have method implicitly
return a Solid
?
这篇关于条件隐式定义Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文