包含 bang 的方法在 REPL 中不起作用 [英] Methods containing bang doesn't work in REPL

查看:43
本文介绍了包含 bang 的方法在 REPL 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 Scala 2.9.2 REPL,如果我复制并粘贴以下方法:

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).

有什么办法可以让这个方法在 REPL 中工作吗?

Is there any way to make the method work in the REPL?

推荐答案

您可能已经错过了这个实例:

You might have missed this instance:

https://issues.scala-lang.org/browse/SI-7650

但是 paulp 修复不向后兼容.

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

而不是在 2.11 上崩溃:

as opposed to crashing on 2.11:

scala> 1 != 2
java.lang.IllegalArgumentException: != 2: event not found

这篇关于包含 bang 的方法在 REPL 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆