Why does the scala-ide not allow multiple package definitions at the top of a file?

StackOverflow https://stackoverflow.com/questions/19284720

  •  30-06-2022
  •  | 
  •  

문제

In scala it is common practice to stack package statements to allow shorter imports, but when I load a file using stacked packages into the scala ide and I attempt to use an import starting with the same organization I get a compiler error from what appears to be the presentation compiler. The code compiles fine in sbt outside of the IDE.

An example code snippet is as follows:

package com.coltfred
package util
package time

import com.github.nscala_time.time.Imports._

On the import I get the error object github is not a member of package com.coltfred.util.com.

If I move the import to a single line the error will go away, but we've used this practice frequently in our code base so changing them all to be single line package statements would be a pain.

Why is this happening and is there anything I can do to fix it?

Edit:

I used the eclipse-sbt plugin to generate the eclipse project file for this. The directory structure is what it should be and all of the dependencies are in the classpath.

Edit 2:

It turns out there was a file in the test tree of the util package (which should have been in the same package), but had a duplicate package statement at the top. I didn't check the test tree because it shouldn't affect the compilation of the main tree, but apparently I was wrong.

도움이 되었습니까?

해결책 2

This is a common annoyance that annoyed paulp into an attempt to fix it. His idea was that a dir that doesn't contribute class files shouldn't be taken as a package. If you can take util as scala.util, you should do so in preference to foo.util where that util is empty.

The util dir is the usual suspect, because who doesn't have a util dir lying around, and in particular, ./util?

apm@mara:~/tmp/coltfred$ mkdir -p com/coltfred/util/time
apm@mara:~/tmp/coltfred$ mkdir -p com/coltfred/util/com
apm@mara:~/tmp/coltfred$ vi com/coltfred/util/time/test.scala
apm@mara:~/tmp/coltfred$ scalac com/coltfred/util/time/test.scala
./com/coltfred/util/time/test.scala:5: error: object github is not a member of package com.coltfred.util.com
import com.github.nscala_time.time._
           ^
one error found
apm@mara:~/tmp/coltfred$ cat com/coltfred/util/time/test.scala
package com.coltfred
package util
package time

import com.github.nscala_time.time._

class Test

apm@mara:~/tmp/coltfred$ 

To debug, find out where the offending package is getting loaded from.

다른 팁

Not sure why the Scala IDE is not liking this, but you can force the import to start at the top level using _root_:

import _root_.com.github.nscala_time.time.Imports._

See if that avoids irritating the IDE.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top