scala-ide / scala-worksheet

A Scala IDE plugin for a multi-line REPL (called worksheet)
96 stars 24 forks source link

toString on a negative number gives a compile error #160

Open davidallsopp opened 11 years ago

davidallsopp commented 11 years ago

If I create the following worksheet

object negative {
    2.toString                                      //> res0: String = 2
   (2).toString                                    //> res1: String = 2
   // compile error:
   (-2).toString
}

the final line causes a compile error:

';' expected but ')' found. illegal start of simple expression

However, the same three lines compile and run fine within a normal Scala source file.

This is using Eclipse 3.7.2, Scala IDE 3.0.0.v-2_10, Scala Worksheet 0.1.4.v-2_10

davidallsopp commented 11 years ago

A Stack Overflow answer on this issue says that

"The code in the main object (the first one) of a worksheet is instrumented before being executed. In the 2 mentioned case, the result of the instrumentation is not valid Scala code.But it is only a problem if the code is at the top level in the main object. If the code is moved to a function or a different object in the same file, it works fine."

I notice that it also works if I wrap the code in an block { } within the main object.