user4522478
user4522478

Reputation:

How to call a function in another file in Scala?

I am trying to figure out how to split up a scala program into multiple files. My background is mostly C programming. I want to learn how to do the equivalent of C's #include in Scala.

I am trying as follows:

package test

object HelloObject {
  def main(args: Array[String]) {
    HelloTwoObject.foo()
  }
}

and in another file I have

package test

object HelloTwoObject {
  def foo(){
    println("Hello Foo!")
  }
}

Then I do scalac hello.scala hellotwo.scala

Then I go into the folder test and try scala HelloObject.class, but it doesn't work. It gives me a long error

What is wrong here?

Upvotes: 2

Views: 8921

Answers (2)

mithunpaul
mithunpaul

Reputation: 3526

The specific issue in your case is the wrong compilation command, like @KimStebel said above.

But to answer your specific question:as long as both the files/functions/objects are within the same package,(which you are already doing, as package test) it will automatically link.

Upvotes: 0

Kim Stebel
Kim Stebel

Reputation: 42047

The scala executable takes a script file or an object name as an argument, but HelloObject.class is neither.

It should just be scala test.HelloObject.

Upvotes: 4

Related Questions