Why do I need semicolons after these imports?

泪湿孤枕 提交于 2019-12-10 14:38:44

问题


I never really used Traits much in Scala so far, and I want to change this. I have this code:

import tools.nsc.io.Path
import java.io.File

trait ImageFileAcceptor extends FileAcceptor {
    override def accept(f:File) =  {
        super.accept(f) match {
            case true => {
                // additional work to see if it's really an image
            }
            case _ => false
        }
    }
}

The problem is, when I compile with sbt, I keep receiving:

ImageFileAcceptor.scala:2: ';' expected but 'import' found.

If I add ; after the imports, the code compiles. Here's FileAcceptor:

import java.io.File

trait FileAcceptor extends Acceptable {
    override def accept(f:File):Boolean = f.isFile
}

And here's Acceptable:

import java.io.File

trait Acceptable {
    def accept(f:File):Boolean
}

I don't understand why I need semicolons after the imports.

Maybe the output of sbt is helpful:

[info] Building project tt 1.0 against Scala 2.8.1
[info]    using sbt.DefaultProject with sbt 0.7.5 and Scala 2.7.7

回答1:


When the scala compiler encounters a Macintosh line ending--being \r--the scala compiler will erroneously declare the need for a semi-colon, as Moritz so deduced. Section 1.2 of the Scala Reference Manual describe correct newline characters. I could not find in the Reference which character literals were considered as newlines. From experience, both Windows (\r\n) and Unix (\n) are acceptable. Presumably scala is strictly compatible with Java in this regard.



来源:https://stackoverflow.com/questions/5748451/why-do-i-need-semicolons-after-these-imports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!