2 在不同 类 中具有相同名称的扩展方法在 Scala 3 中不起作用?
2 Extension Methods with the same name in different classes do not work in 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:
_
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:
|
| _
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
我有以下场景:
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:
_
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:
|
| _
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