简体   繁体   English

Scala 14:错误:设备不带参数

[英]Scala 14: error: Unit does not take parameters

New to scala world and for exercise , executed below lines of code in windows REPL. 在scala世界中,它是锻炼的新手,在Windows REPL中的代码行以下执行。 getting error Unit does not take parameters.Any idea on this. 出现错误Unit不接受参数。对此有任何想法。

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>

That is because scala thinks println() is taking another parameter when it sees {} . 这是因为scala看到{}时认为println()正在采用另一个参数。 You simply can test with following code as well, 您也可以使用以下代码进行测试,

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

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

You need to put a new line between println() and {} to make compiler happy. 您需要在println(){}之间插入新行,以使编译器满意。

example: https://scastie.scala-lang.org/prayagupd/jbPWBesyTvihwue8soE5Og 示例: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM