Scala 公共方法:';'预期但“def”成立
我写了这个方法:
public def getXScaleFactor(panelWidth: Int): Double = {
return (panelWidth / (samplesContainer[0].length.asInstanceOf[Double]))
}
并且我在编译时遇到问题:
[error] ./src/main/scala/Controllers/TrackController.scala:85: ';' expected but 'def' found.
[error] public def getXScaleFactor(panelWidth: Int): Double {
[error] ^
这段代码有什么问题?
I wrote this method:
public def getXScaleFactor(panelWidth: Int): Double = {
return (panelWidth / (samplesContainer[0].length.asInstanceOf[Double]))
}
and I have problems with compilation:
[error] ./src/main/scala/Controllers/TrackController.scala:85: ';' expected but 'def' found.
[error] public def getXScaleFactor(panelWidth: Int): Double {
[error] ^
What is wrong in this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
public
不是 Scala 中的保留字,因此它将其解释为变量名。默认为公共访问;只要去掉public
就可以了。public
is not a reserved word in Scala, so it's interpreting it as a variable name. Public access is the default; just leave offpublic
and you'll be fine.方法默认是公共的。删除
public
。Methods are public by default. Remove
public
.只是为了添加上面的答案:
您还可以删除
return
关键字。函数/方法中的最后一个语句/表达式自动成为返回值。Just to add up to the answers above:
You can also remove
return
keyword. The last statement/expression in a function/method is automatically the return value.问题在于您已经编写了 Java 代码。
除了
public
之外,您还使用[]
对集合进行索引访问(这是无效的)、显式返回类型(这不是必需的),return
(这也是不需要的)和.asInstanceOf
(这是不必要的,并且有代码味道)尝试这个以获得轻量级、更惯用的体验:
或者如果 < code>samplesContainer 可能为空:
放入任何内容更喜欢代替默认的
42
The problem is that you've written Java code.
As well as
public
, you've also used[]
for indexed access to a collection (which is invalid), an explicit return type (which isn't needed),return
(which also isn't needed), and.asInstanceOf
(which is unnecessary, and a code smell)Try this for a lightweight, more idiomatic experience:
Or if
samplesContainer
might be empty:Put whatever you prefer in place of the default
42
there