pme*_*pme 6 extension-methods scala dotty 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}")
Run Code Online (Sandbox Code Playgroud)
这给了我以下编译异常:
value doit is not a member of B.
An extension method was tried, but could not be fully constructed:
_$1
Run Code Online (Sandbox Code Playgroud)
Scala 3中扩展方法的命名有限制吗?
请参阅Scastie上的示例
将评论变成答案,规范说这两个扩展都被翻译为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
Run Code Online (Sandbox Code Playgroud)
这与调试原始示例时出现的错误相同:
>>>> 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
Run Code Online (Sandbox Code Playgroud)
过载分辨率得到了明确的改进或扩展,以处理这种正常的重载情况,这些重载可能仅在后面的参数列表中进行区分。所以显然我们应该意识到脱糖形式是超载的。
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
726 次 |
| 最近记录: |