我有以下场景:
case class B(v: String)
case class A(bs: Seq[B])
extension(a: A)
def doit() = a.bs.map(_.doit()) // here is the exception
extension(b: B)
def doit() = println("OK: ${b.v}")
这给了我以下编译异常:
value doit is not a member of B.
An extension method was tried, but could not be fully constructed:
_$1
在那儿限制在扩展方法的命名中Scala 3?
请参阅此处的示例Scastie https://scastie.scala-lang.org/dUp8oopfSji1RCMvL8gTnA
将评论转化为答案,spec https://dotty.epfl.ch/docs/reference/contextual/extension-methods.html说两个扩展名都被翻译成def extension_doit
.
显式调用扩展方法:
-- [E044] Cyclic Error: so.scala:7:45 -----------------------
7 | extension(a: A) def doit() = a.bs.map(b => extension_doit(b)())
| ^
| Overloaded or recursive method extension_doit needs return type
这与调试原始示例时出现的错误相同:
>>>> StoredError: Overloaded or recursive method extension_doit needs return type
[snip]
-- [E008] Not Found Error: so.scala:7:42 --------------------
7 | extension(a: A) def doit() = a.bs.map(_.doit())
| ^^^^^^
| value doit is not a member of B.
| An extension method was tried, but could not be fully constructed:
|
| _$1
过载分辨率 https://dotty.epfl.ch/docs/reference/changed-features/overload-resolution.html被显式改进或扩展以处理这种正常的重载情况,这些重载可能仅在后面的参数列表中进行区分。所以显然我们应该意识到脱糖形式是超载的。
Scala 2 也抱怨:
scala> object X { def f(i: Int) = f("") ; def f(s: String) = f(42) }
^
error: overloaded method f needs result type
^
error: overloaded method f needs result type
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)