问题
I am running Scala 2.9.2 REPL and if I copy&paste following method:
def isPrime(num: Int): Boolean = {
val ceiling = math.sqrt(num.toDouble).toInt
(2 to ceiling) forall (x => num % x != 0)
}
..from the file with a source code (where it works well) to the Interactive Interpreter. I get this exception:
java.lang.IllegalArgumentException: != 0): event not found
at jline.console.ConsoleReader.expandEvents(ConsoleReader.java:426)
...
The problem is the !
character (methods without exclamation mark works well).
Is there any way to make the method work in the REPL?
回答1:
You might have missed this instance:
https://issues.scala-lang.org/browse/SI-7650
But the paulp fix isn't backward compatible.
scala> :power
** Power User mode enabled - BEEP WHIR GYVE **
** :phase has been set to 'typer'. **
** scala.tools.nsc._ has been imported **
** global._, definitions._ also imported **
** Try :help, :vals, power.<tab> **
scala> $r.r.in.asInstanceOf[scala.tools.nsc.interpreter.JLineReader].consoleReader.setExpandEvents(false)
scala> 1 != 2
res1: Boolean = true
as opposed to crashing on 2.11:
scala> 1 != 2
java.lang.IllegalArgumentException: != 2: event not found
回答2:
I wasn't able to overcome this issue with the original installation, but installing new version of Scala helped. Perhaps, it is issue of Fedora 17 rpm Scala package.
- Related bug
- rpm package with "broken" REPL
来源:https://stackoverflow.com/questions/17630511/methods-containing-bang-doesnt-work-in-repl