【问题标题】:Scala 14: error: Unit does not take parametersScala 14:错误:单元不带参数
【发布时间】:2019-03-08 13:14:51
【问题描述】:

Scala 世界的新手和练习,在 windows REPL 中的代码行下面执行。得到错误单元不接受参数。对此有任何想法。

scala> :paste
// Entering paste mode (ctrl-D to finish)

val x:Int = 10
println(x)
{
val x:Int =20
println(x)
}
println(x)

// Exiting paste mode, now interpreting.

<pastie>:14: error: Unit does not take parameters
{
^

scala>

【问题讨论】:

标签: java scala


【解决方案1】:

这是因为 scala 在看到 {} 时认为 println() 正在使用另一个参数。您也可以使用以下代码进行测试,

scala> println(8){}
                 ^
       error: Unit does not take parameters

scala> println(1)()
                 ^
       error: Unit does not take parameters

您需要在println(){} 之间添加一个新行以使编译器满意。

示例:https://scastie.scala-lang.org/prayagupd/jbPWBesyTvihwue8soE5Og

scala> :paste
// Entering paste mode (ctrl-D to finish)

val x:Int = 10
println(x)

{
val x:Int =20
println(x)
}
println(x)

// Exiting paste mode, now interpreting.

10
20
10
x: Int = 10

【讨论】:

  • 在末尾加上分号println(x); 也可以
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-21
  • 2021-05-02
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
相关资源
最近更新 更多