Tower
Tower

Reputation: 102935

Scala -- not a member of package

I am learning Scala so bear with me if this is a stupid question.

I have this package and a class (teared it down to most simplistic version):

package Foo {
  class Bar {}
}

then in main.scala file I have:

import Foo.Bar

object test {
  def main() {
    val b = new Bar()
  }
}

Why am I getting this:

test.scala:1: error: Bar is not a member of Foo

It points at the import statement.

Upvotes: 2

Views: 6552

Answers (3)

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297265

The most likely explanation is that you did not compile the first file, or you are doing something wrong when compiling. Let's say both files are in the current directory, then this should work:

scalac *.scala

It should generate some class files in the current directory, as well as a Bar.class file in the Foo directory, which it will create.

Upvotes: 1

Luigi Plinge
Luigi Plinge

Reputation: 51109

scalac is the scala compiler. Foo.bar needs to have been compiled for you to use it, so you can't just run your main.scala as a script.

The other mistake in your code is that the main method needs to be

def main(args: Array[String]) {  ...

(or you could have test extends App instead and do away with the main method).

I can confirm if you put the two files above in an empty directory (with the correction on the main method signature) and run scalac * followed by scala test it runs correctly.

Upvotes: 3

VonC
VonC

Reputation: 1328522

To quickly test a scala code in IntelliJ (with the Scala plugin), you can simply type Ctrl+Shift+F10:

test code in Scala

Note that for testing a Scala class, you have other choices, also supported in IntelliJ:

Upvotes: 0

Related Questions