I am trying to use Scala 3 as scripting language now, encountered few obstacles, so i wonder what is the right way to do it.
I am using the latest “Scala code runner version 3.1.2 – Copyright 2002-2022, LAMP/EPFL”.
First thing I did, wrote a one line script Hello.scala and executed it:
scala Hello.scala
It was too slow, a few seconds, so I realized it compiles it every time. Then I found somebody’s suggestion to use ‘-savecompiled’ flag:
scala -savecompiled Hello.scala
This actually created Hello.jar file in the same directory, but the run was still slow. I realized that it does not use the Hello.jar file for the run. So then I tried this:
java -jar Hello.jar
This worked and it was fast. But do I really need to do such workaround? By the way, when I ran ‘scala -help’, I did not see ‘-savecompiled’ in the options list, so it looks like undocumented feature. What other features are there undocumented?
Here is my first question: maybe there is an option to tell scala to use the saved jar file if it exists and up to date?
Next thing I tried: reusing code defined in another scala file. Here are my two files:
Util.scala
object Util:
def utiltest() = println("This is utiltest")
Hello.scala
import Util._
@main def hello =
println("Hello, world")
utiltest()
The ‘scala Hello.scala’ did not work, and ‘scala -cp . Hello.scala’ did not work either. The only way to make it work was to use scalac first to compile both files (or at least Util.scala) and then use ‘-cp’ when running it.
The good news was, if I also use ‘-savecompiled’, it created Hello.jar that contained everything that is needed to run the program.
Here is my second question: is there any way to tell scala to search for dependencies, even if they are source files (this is the whole point of scripting)?