2

The Scala expression

.3.+(5)

evaluates to 5.3 as a result in Ammonite-REPL, as I expected. In the Scala REPL, though, it yields a syntax error, printing

scala> .3.+(5)
<console>:1: error: ';' expected but double literal found.
       $intp.3.+(5)
            ^

Intuitively, .3.+(5) seems to be a valid expression to me. Is there a bug in Ammonite or is it in the Scala REPL?

spilot
  • 625
  • 1
  • 7
  • 20

2 Answers2

4

That particular expression doesn't work in the Scala REPL, because it has a feature that allows you to call methods on the last expression that was output. The last expression in your REPL session must have been named $intp. This feature is typically used like this:

scala> List(1, 2, 3, 4)
res14: List[Int] = List(1, 2, 3, 4)

scala> .map(_ + 1)
res15: List[Int] = List(2, 3, 4, 5)

In your case, the Scala REPL thinks you're trying to call a method named .3 on the last output. I can't find anything in the Ammonite docs or by trying it out that suggests it supports this feature. That is, Ammonite doesn't support the example I pasted above, which is probably the less confusing way to do things.

Michael Zajac
  • 55,144
  • 7
  • 113
  • 138
2

When you start an expression with a . in the regular Scala REPL, it tries to interpret it as though you try to invoke a method on the previous evaluation result.

So when you write .3.+(5) the REPL thinks you meant resX.3.+(5), with resX being whatever was the last result the REPL returned.

Jasper-M
  • 14,966
  • 2
  • 26
  • 37