为什么在Scala中使用带有`to`的变量是不可能的?

Jar*_*ler 1 collections scala range

以下内容无法编译:

var id_last_pivot : Integer = 0
(id_last_pivot to content.length).maxBy(...
Run Code Online (Sandbox Code Playgroud)

报告的错误是:"无法解析符号"

如果我用id_last_pivot数字值替换,它可以工作.

为什么?如何解决这个问题?

Sha*_*ala 9

Integer是从java该包装在一个对象的原始类型的值,并且没有任何方法to在其

您需要Int从包scala.Int中使用该方法to

var id_last_pivot : Int = 0
Run Code Online (Sandbox Code Playgroud)

现在您可以使用该方法 (id_last_pivot to content.length)

来自[[scala.Int]]=> 的隐式转换[[scala.runtime.RichInt]] 提供了有用的非基本操作.

希望这可以帮助!