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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...