负数上的 toString 无法在 Scala 工作表中编译 [英] toString on a negative number doesn't compile in Scala Worksheet

查看:74
本文介绍了负数上的 toString 无法在 Scala 工作表中编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 Eclipse 中创建一个 Scala 工作表,如下所示:

If I create a Scala Worksheet in Eclipse as follows:

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

但是,相同的三行代码在普通 Scala 源文件中编译和运行良好.

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

为什么这在工作表中不起作用?

Why does this not work in the worksheet?

这是使用 Eclipse 3.7.2、Scala IDE 3.0.0.v-2_10、Scala Worksheet 0.1.4.v-2_10

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

[更新:这个问题本来是用toBinaryString的,但是用toString也有问题,所以我简化了]

推荐答案

这是一个错误.工作表的主对象(第一个)中的代码在执行之前被检测.在提到的 2 种情况下,检测结果不是有效的 Scala 代码.

It is a bug. 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.

这篇关于负数上的 toString 无法在 Scala 工作表中编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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