0

New to scala world and for exercise , executed below lines of code in windows REPL. getting error Unit does not take parameters.Any idea on this.

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>
Learn Hadoop
  • 2,760
  • 8
  • 28
  • 60
  • Possible duplicate of [Scala: "recursive value ... needs type" but I use Java types only](https://stackoverflow.com/questions/35013183/scala-recursive-value-needs-type-but-i-use-java-types-only) – Brian McCutchon Mar 08 '19 at 07:15
  • Another dupe target: https://stackoverflow.com/questions/52017702/when-is-semicolon-mandatory-in-scala/52019261#52019261 – Brian McCutchon Mar 08 '19 at 07:45

1 Answers1

8

That is because scala thinks println()is taking another parameter when it sees {}. 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.

example: 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
prayagupa
  • 30,204
  • 14
  • 155
  • 192