scala - Function0 上的 Scala 堆栈修改
问题描述
我刚刚在 Scala 的堆栈修改中遇到了非常奇怪的行为。
让我们看一个例子:
class Base extends Function0[Unit] {
override def apply(): Unit = println("base")
}
trait Modification extends Function0[Unit] {
abstract override def apply(): Unit = { print("modified "); super.apply() }
}
val a = new Base with Modification
a.apply()
我希望输出像modified base
. 相反,我得到modified modified modified...
了一个StackOverflowError
有趣的是,这只发生在 Function0[Unit] 中。功能 [字符串] 工作正常:
class Base extends Function0[String] {
override def apply(): String = "base"
}
trait Modification extends Function0[String] {
abstract override def apply(): String = "modified " + super.apply()
}
val a = new Base with Modification
a.apply()
输出:
defined class Base
defined trait Modification
a: Base with Modification = <function0>
res0: String = modified base
有人可以解释这种行为吗?
解决方案
看起来像一个错误,因为自定义Function0
一切都按预期工作
trait Function0[+R] {
def apply(): R
}
class Base extends Function0[Unit] {
override def apply(): Unit = println("base")
}
trait Modification extends Function0[Unit] {
abstract override def apply(): Unit = { print("modified "); super.apply() }
}
val a = new Base with Modification
a.apply() // modified base
应在此处报告错误:https ://github.com/scala/bug/issues
推荐阅读
- jquery - 检查我是否在表格的最后一行触发了选择
- r - 通过组合出现在其他数据框中的 2 个变量来过滤数据框
- bash - 从 telnet 会话中提取 eth0 的 IP 地址
- java - ClassCastException:将 IBM MQ 配置为 Flume JMS 源时,无法将 javax.naming.Reference 强制转换为 javax.jms.ConnectionFactory
- python - Python中的二维列表重复随机数分配
- .net - 当我单击按钮时,Select Case 不会做任何事情
- docker - Docker:使用 --placement-pref 在特定节点上运行服务
- reactjs - useState 钩子如何能够重新渲染其父函数?
- java - 垂直布局时不显示可扩展字符串
- javascript - Debian 服务器 Node -v v10.15.3 不支持 Js Spread 语法 ([...])