Scala REPL. First it says "illegal start of definition", then "error: not found: type" when I try it a different way

Scala REPL. First it says "illegal start of definition", then "error: not found: type" when I try it a different way

对不起各位 - 我知道这些问题在 Scala 论坛上被问了很多,但我已经尝试在此处关注 posts,但我 运行 遇到了障碍,因此,如果您能提供帮助,我将不胜感激。

我在 Windows 命令行终端上使用 Scala REPL。我学习了 Jason Swartz 的“学习 Scala”并尝试了基本练习,效果很好。因此,我尝试了“Scala Cookbook”作者在此网络博客上列出的抛硬币 Scala 游戏(他的最终代码块从标题“完整源代码”开始): https://alvinalexander.com/scala/fp-book/functional-game-with-a-little-state/

我将两个代码块复制并粘贴到记事本中,并将这两个文件另存为 CoinFlipUtils.scala 和 coinflip.scala 以及 运行 第一个使用:

scala CoinFlipUtils.scala

但是我收到关于包的错误消息:

CoinFlipUtils.scala:1: error: illegal start of definition

package com.alvinalexander.coinflip.v1

所以我查看了 Whosebug 网站,人们说改为使用 :paste -raw,然后将完整代码粘贴到粘贴模式以避免包错误消息。所以我使用 CoinFlipUtils 代码做到了这一点,但现在我收到了这些错误消息:

// Exiting paste mode, now interpreting.
def printGameState(printableFlipResult: String, gameState: GameState): Unit = {
                                                           ^
    def printGameState(gameState: GameState): Unit = {
                                  ^
<pastie>:22: error: not found: type GameState
    def getUserInput(): String = readLine.trim.toUpperCase
                                 ^
    <pastie>:10: warning: Auto-application to `()` is deprecated. Supply the empty argument list `()` explicitly to invoke method readLine,
    or remove the empty argument list from its definition (Java-defined methods are exempt).
    In Scala 3, an unapplied method like this will be eta-expanded into a function.
    There were compilation errors!

所以我不确定从这里到哪里去。博客上的代码 post 是否已过时,或者我只是错误地设置了 Scala REPL?我只是不明白。教科书中的例子更容易,所以现在我很困惑试图让一个真正的程序运行。谢谢。

关于如何更容易地在 REPL 或脚本中输入一些任意代码,然后逐步迁移到工作表或独立代码,有一些问题或讨论。

这里描述的票是https://github.com/scala/bug/issues/4768,比较旧

可能 REPL 更多地用于较小的片段; Ammonite shell 用于更强大的脚本。

您可以预编译一些来源:

$ scalac -d ab.jar a.scala b.scala
$ scala -classpath ab.jar c.scala

其中 a.scala 等具有任意来源,并且 c.scala 具有“顶级脚本”语句。

或者,

scala -i a.scala -i b.scala
Welcome to Scala 2.13.5 (OpenJDK 64-Bit Server VM, Java 16.0.1).
Type in expressions for evaluation. Or try :help.

scala> new b.B(new a.A)
val res0: b.B = b.B@2349f14d

其中源文件有普通包。