Scala - 分号不一致 [英] Scala - semicolon inconsistency

查看:88
本文介绍了Scala - 分号不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个非常简单的代码(scala-2.10):

I have this very simple code (scala-2.10):

import scala.io.Source

object Test2 {
    def main(args: Array[String]): Unit = {
        for(line <- Source.fromFile("/Users/alexei/words.txt", "utf-8").getLines()) {
            println(line)
        }
    }
}

我在编译时收到此错误消息:

I get this error message when compiling:

Test2.scala:3: error: ';' expected but 'object' found.
object Test2 {
^
one error found

我非常困惑何时使用分号.我还有其他与此类似的代码,并且在没有任何分号的情况下编译没有任何问题.

I am terribly confused when to use semicolons or not. I have other code similar to this and it has no issues compiling without any semicolons what so ever.

有人可以解释这个特定的错误并详细说明需要分号的所有情况吗?

Can someone please explain this specific error and detail all the situations where a semicolon is needed?

推荐答案

代码对我来说很好用.

唯一需要分号的时候是在同一行写多条语句.否则,换行符表示分离.因此,您当前的代码中不需要任何分号,但如果您想这样做:

The only time you need semicolons is when you are writing multiple statements on the same line. Otherwise the line-break indicates the separation. So you don't need any semicolons in your current code, but if you would if you wanted to do this:

println(line); println(line)

而不仅仅是

println(line)
println(line)

这篇关于Scala - 分号不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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