诊断Scala编译错误“值为不是Int的成员” [英] Diagnosing Scala compile error "value to is not a member of Int"

查看:168
本文介绍了诊断Scala编译错误“值为不是Int的成员”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个工作正常的Scala类中进行了代码更改。在尝试编译修改时,编译器向这个(预先存在的)代码行中输出错误消息值到不是Int的成员:

I made a code change within a Scala class that had been working fine. Upon trying to compile the modification, the compiler spit out the error message, "value to is not a member of Int" relating to this (pre-existing) line of code:

for (i <- 0 to cColumn -1) { ... }

做一些研究,我遇到了一些关于to方法的错误报告,并且to显然是intWrapper类(?)中提供的一个方法。

Doing some research, I came across some bug reports on the "to" method - and also that "to" is apparently a method provided within the intWrapper class(?).

所以,基于这些信息,我开始查看我的类的import语句...没有这样的intWrapper的导入。 (问:这种情况下,这是如何编译/运行在第一个地方?)什么使这更令人感兴趣(对我)是,当我开始在代码库中的全局搜索导入我意外终止编译器(sbt)会话...但是当我重新启动它,类编译就好了。没有错误。 (并且没有上一个会话的代码更改)



任何人都有什么想法会导致这种间歇性行为?

So, based upon that info, I started looking at my class's import statements... no such import for intWrapper. (Q: That being the case, how did this ever compile/run in the first place?) What makes this even more interesting (to me) is that when I started to do a global search in the codebase for that import I accidentally terminated the compiler (sbt) session...but when I restarted it, the class compiled just fine. No errors at all. (And no code changes from the previous session)

Anyone have any ideas as to what would cause this intermittent behavior?

注意:

1)使用Scala 2.10.2和javac 1.7.0_25

2)类的代码更改与示例功能无关,也没有修改任何类的导入

NOTES:
1) using Scala 2.10.2 with javac 1.7.0_25
2) the code change to the class had nothing to do with the example functionality, nor did it alter any of the class's imports

更新:以下是变量声明:

Update: Here are the variable declarations:

val meta = rs.getMetaData()
val cColumn = meta.getColumnCount()

EDIT:根据建议,这里是测试行(现在都编译好了):

Per suggestion, here is the test lines (all of them compile fine now):

implicitly[scala.Int => scala.runtime.RichInt]
intWrapper(3) to 4
for (i <- 0 to 33 -1) { /* do something smart */ }

for (i <- 0 to cColumn -1) { ... }

以下是完整的编译器错误:

EDIT 2 Here is the full compiler error:

[error] /path/to/src/file/DBO.scala:329: value to is not a member of Int
[error]       for (i <- 0 to cColumn -1) {
[error]

该错误在类中重复〜18次。 (这是一个DBO-DB接口层);其中DBO.scala是包含新修改的trait的文件。

That error was repeating ~18 times in the class. (It's a DBO-DB interface layer); where DBO.scala is the file containing the newly modified trait.

推荐答案

我刚遇到同样的问题。在我的例子中,它是由不必要的 import 引起的,如下所示:

I just encountered this same issue. In my case, it was caused by an unnecessary import, like this:

import scala.Predef.String

class Test() {
  for (t <- 1 to 3) {}
}  

默认情况下,Scala导入所有 scala.Predef Predef 扩展 LowPriorityImplicits ,其中包含 Int 到实际上定义在

By default, Scala imports all of scala.Predef. Predef extends LowPriorityImplicits, which includes an implicit conversion from Int to RichInt.

code> RichInt ,因此您需要进行此转换才能使用它。通过导入 Predef 的一部分,我将失去此转换。删除不必要的 import ,错误消失。

to is actually defined on RichInt, so you need this conversion in order to use it. By importing just part of Predef, I lose this conversion. Get rid of the unnecessary import and the error goes away.

这篇关于诊断Scala编译错误“值为不是Int的成员”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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