Diagnosing Scala compile error “value to is not a member of Int”

北战南征 提交于 2019-12-06 07:11:14

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) {}
}  

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

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.

how did this ever compile/run in the first place?

By default, the contents of scala.Predef is imported. There you have method intWrapper which produces a RichInt with method to.

You probably have shadowed symbol intWrapper. Does the following work:

implicitly[scala.Int => scala.runtime.RichInt]

or this:

intWrapper(3) to 4

...if not, there lies your problem.


EDIT: So, since you say that compiles, what happens is you replace cColumn with a constant, e.g.

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

? It would also help to post the complete compiler message with indicated line etc.

Without knowing where that error comes from, you might also try to work around it by constructing the Range by hand:

for (i <- Range.inclusive(0, cColumn-1)) { ... }

or

Range.inclusive(0, cColumn-1).foreach { i => ... }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!