scala - 模式类型与教科书示例中的预期类型不兼容
问题描述
我正在阅读 Underscore 的 Essential Scala 教科书,当我尝试使用 :load 命令从命令行使用其中一个示例时,我得到了错误。[编辑] 我说的是命令行,我的意思是控制台。
我已经将代码本身放入一个文件中,然后尝试使用 :load 命令来使用它。
sealed trait IntList {
def product: Int =
this match {
case End => 1
case Pair(hd, tl) => hd * tl.product
}
}
case object End extends IntList
final case class Pair(head: Int, tail: IntList) extends IntList
我希望代码能够编译并可用,但我收到以下消息:
error: pattern type is incompatible with expected type;
found : End.type
required: IntList
case End => 1
和
error: constructor cannot be instantiated to expected type;
found : Pair
required: IntList
case Pair(hd, tl) => hd * tl.product
解决方案
在 Scala REPL 中使用:paste
命令:
scala> :paste
// Entering paste mode (ctrl-D to finish)
sealed trait IntList {
def product: Int =
this match {
case End => 1
case Pair(hd, tl) => hd * tl.product
}
}
case object End extends IntList
final case class Pair(head: Int, tail: IntList) extends IntList
// Exiting paste mode, now interpreting.
defined trait IntList
defined object End
defined class Pair
scala>
ctrl-D
粘贴完所有行后,只需按下 PS 。
推荐阅读
- google-drive-api - 无法更新或删除共享云端硬盘项目的继承权限
- flutter - 如何应用 BackdropFilter 模糊除中心以外的整个背景?
- swift - 有必要使用这个功能来播放声音吗?
- javascript - 如何轻松将 OpenCV.js 导入 .js 文件?
- javascript - 在 cloudfunction 中运行的 ffmpeg 静默失败/永远不会完成
- font-awesome - 字体真棒图标在移动设备上表现得像薛定谔的猫
- javascript - JavaScript 仅使用脚本标记发送自定义标头
- shell - Shell Scripting:“grep -w”不选择以“-”分隔的单词
- android - 如何使用 AndroidX 在 RecyclerView 中使用插入分隔符?
- c# - Visual Studio C# 应用程序不会使用调试信息构建