如何解决2在不同类中具有相同名称的扩展方法在Scala 3中不起作用?
我有以下情况:
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上查看示例
解决方法
将评论变成答案,spec说两个扩展名都转换为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
Overload resolution已得到显着改进或扩展,以处理这种正常的重载情况,只有在以后的参数列表中才有区别。因此,显然,我们应该知道脱糖形式已超载。
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。