Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 633
Geo Avatar asked Oct 12 '22 05:10

Geo


1 Answers

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.

like image 199
Diego Tejada Avatar answered Oct 23 '22 15:10

Diego Tejada