gpt4 book ai didi

scala - 2 不同类中具有相同名称的扩展方法在 Scala 3 中不起作用?

转载 作者:行者123 更新时间:2023-12-04 13:32:54 27 4
gpt4 key购买 nike

我有以下场景:

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
有吗 限制 中的扩展方法的命名中斯卡拉 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

关于scala - 2 不同类中具有相同名称的扩展方法在 Scala 3 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64016090/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com