2
试图了解这种隐式发现的情况 - finding implicit of an argument's type。我复制粘贴正式例如到IDE中,只是改变了方法名MUL这样的:斯卡拉隐式参数的类型查找失败
class A(val n: Int) {
def mul(other: A) = new A(n + other.n)
}
object A {
implicit def fromInt(n: Int) = new A(n)
}
1 mul (new A(1))
现在,它会导致编译错误说:
value mul is not a member of Int
我也尝试过用字符串代替Int,这又导致编译错误。
你能解释我做错了什么部分吗?
哇,非常感谢!这正是问题所在。应用隐式之前,方法名称应该已存在于对象上。 –
非常有趣,当这种分辨率会有用吗? – Mikel
@Mikel;这是个好问题。我不知道为什么/隐含的范围规则是为什么决定的。对于了解这一点我有点惊讶。 – jwvh