Skip to content

Instantly share code, notes, and snippets.

@ngpestelos
Last active December 15, 2015 15:09
Show Gist options
  • Save ngpestelos/5279320 to your computer and use it in GitHub Desktop.
Save ngpestelos/5279320 to your computer and use it in GitHub Desktop.
Scala import classes

See: http://stackoverflow.com/questions/3075951/scala-importing-class

I have a file called CashFlow.scala that I wish to use with another object Hello.scala. Both are in the same directory:

class CashFlow(amt: Double, curr: String) {
  def this(amt: Double) = this(amt, "GBP")
  def doSomething {
    println "do something"
  }
}

object Hello extends App {
  println "In Hello"
  val c = new CashFlow(2000.0, "PHP")
  c.doSomething
}

While these files can be run in the REPL, I like running them same as a Java file (automatically calls Hello's main method).

Here's how I did it. First, I compiled the classes:

$ scalac *.scala

Then I ran Hello:

$ scala -cp . Hello

I've been working on interpreted languages for too long that I forgot about the compilation step. I wish there was a way to automatically compile a Scala class when changes are made to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment