質問

I am working on a small Scala project. I have the following issue with 'import':

If, at the top of one of my files, I import two thing with these commands:

import main.Main._
import main.game.Game
       ^^^^

it gives me the following error message at the underlined 'main' word: "missing arguments for method main in object Main; follow this method with `_' if you want to treat it as a partially applied function" which is quite strange especially that it is just an import statement. And naturally no actual importing occures. At first I thought about semicolon inference quirks again but it is not the case. If I swap the two lines and write like this:

import main.game.Game
import main.Main._

then everythinng is fine.

Could anyone shed some light on that? Is it something special about Scala?

役に立ちましたか?

解決

Presumably you have a main method in object Main. So after import main.Main._ main refers to this method instead of the main package. You could avoid it in several ways:

  1. Change import order, as in the question.
  2. Don't import the main method, as Daniel C. Sobral's answer suggests.
  3. Explicitly say you want the top-level main package:

    import _root_.main.game.Game
    

Following the normal Java package naming convention should avoid this problem in most cases, as you are unlikely to have members (or subpackages) called com or org (though net could be a problem).

他のヒント

You do have a method named main inside main.Main, don't you? Well, since you imported it, it has now shadowed the package by the name main. You can try this to confirm:

import main.Main.{main => _, _}
import main.game.Game

This will exclude main from being imported.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top