Geo
Geo

Reputation: 96797

Why do I need semicolons after these imports?

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

Upvotes: 5

Views: 1945

Answers (1)

troutwine
troutwine

Reputation: 3821

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.

Upvotes: 4

Related Questions