1
votes

I am new to scala and getting below exception while using :paste command in REPL

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

1+2
println("welcome to scala world")

// Exiting paste mode, now interpreting.

<console>:11: warning: a pure expression does nothing in statement position; multiline expressions may require enclosing parentheses
       1+2
        ^
welcome to scala world

scala> scala version : Scala code runner version 2.12.3 -- Copyright 2002-2017, LAMP/EPFL and Lightbend, Inc

1
This seems like expected behavior to me. - Seth Tisue
I came to this question because of this Scastie snippet: scastie.scala-lang.org/VFCGmgfjTECkymOJoHW85Q There should be an exception on line 23 and the pure expression warning on line 39. If you replace invalidFunctionF with square, the exception disappears, but the pure expression warning persists... - Alonso del Arte

1 Answers

2
votes

It's not an exception, just a warning that you can ignore. It warns that in paste mode the expression 1+2 has no effect and the result won't be printed. If you would enter both of your lines in normal mode the REPL will print the result of each expression.

scala> 1+2
res1: Int = 3

scala> println("welcome to scala world")
welcome to scala world

The second part of the warning is for the case that you intend a multi-line expression where each line is a valid expression, e.g.

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

1+2
-5

// Exiting paste mode, now interpreting.

<console>:48: warning: a pure expression does nothing in statement position; you may be     omitting necessary parentheses
       1+2
        ^
res1: Int = -5

which is different from

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

(1+2
-5)

// Exiting paste mode, now interpreting.

res22: Int = -2