无法使用:粘贴在Scala 3.0.0 repl中

发布于 2025-01-27 04:13:58 字数 679 浏览 2 评论 0原文

在Scala 2.x中,我使用:粘贴用于多行粘贴。我正在使用Scala 3.0.0,并且使用相同的方法会引发错误:

scala> :paste
Unknown command: ":paste", run ":help" for a list of commands

我尝试了以下内容,但是写完代码后我不知道该如何执行:

scala> if (true)
     |   println("that was true")
     | else
     |   print("false")
     |

我在

:粘贴未实施,但也不是真的需要,因为 Scala 3 repl。

支持多行粘贴。

如何在Scala 3中执行多行代码?

更新1:

我通过使用

scala> if (true)
     |   println("that was true")
     | else
     |   print("false")
     | ;
that was true

是否有其他方法来执行多行代码,而无需编写;

In Scala 2.x I use :paste for multi-line pasting. I am using Scala 3.0.0 and using the same approach throws an error:

scala> :paste
Unknown command: ":paste", run ":help" for a list of commands

I tried the following but I don't know how to execute when I'm done writing the code:

scala> if (true)
     |   println("that was true")
     | else
     |   print("false")
     |

I found at https://users.scala-lang.org/t/solved-cannot-use-paste-in-scala3-repl/7587 that

:paste is not implemented but it’s also not really needed because
multi-line pasting is supported by the scala 3 REPL.

How do I execute the multi-line code in Scala 3?

UPDATE 1:

I achieved it by using

scala> if (true)
     |   println("that was true")
     | else
     |   print("false")
     | ;
that was true

Is there another way to execute the multi-line code, without having to write ;?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青瓷清茶倾城歌 2025-02-03 04:13:58

我完成编写代码

时不知道该如何执行

为您的特定情况编写代码后如何执行,只需在repper中再次按 Enter

scala> if (true)
     |  println("that was true")
     | else
     |  print("false")
     | // <-- press enter here again
that was true

repl中的块

您还可以输入由卷曲括号界定的块(即, {和})。块派上用场,可以在卧室中实现密封特征。

让我们尝试定义对象ab扩展了密封特征lats

scala> sealed trait Level
// defined trait Level
                                                                                      
scala> object A extends Level
-- [E112] Syntax Error: --------------------------------------------------------
1 |object A extends Level
  |       ^
  |       Cannot extend sealed trait Level in a different source file
  |
  | longer explanation available when compiling with `-explain`
1 error found

depp将latver 和a,就好像它们位于不同的文件中一样。如果将它们分组在一个块中,情况并非如此:

scala> {
     | sealed trait Level
     | object A extends Level
     | object B extends Level
     | }
// defined trait Level
// defined object A
// defined object B

I don't know how to execute when I'm done writing the code

For your particular case, just press Enter again in the REPL:

scala> if (true)
     |  println("that was true")
     | else
     |  print("false")
     | // <-- press enter here again
that was true

Blocks in the REPL

You can also enter a block delimited by curly braces (i.e., { and }) in the REPL. Blocks come in handy for implementing sealed traits in the REPL.

Let's try to define the objects A and B that extend the sealed trait Level:

scala> sealed trait Level
// defined trait Level
                                                                                      
scala> object A extends Level
-- [E112] Syntax Error: --------------------------------------------------------
1 |object A extends Level
  |       ^
  |       Cannot extend sealed trait Level in a different source file
  |
  | longer explanation available when compiling with `-explain`
1 error found

The REPL treats the definitions of Level and A as if they were located in different files. That isn't the case if you group them inside a block:

scala> {
     | sealed trait Level
     | object A extends Level
     | object B extends Level
     | }
// defined trait Level
// defined object A
// defined object B
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文