Scala-cli is becoming the default runner in 3.5, via SIP 46, and renamed to just scala. So we’ll be using “sub-commands” like scala compile ..., scala package ..., scala repl ... etc. It’s best to start getting used to it as soon as possible.
I haven’t got scala-cli working on my machine yet, but from the docs could I not write the import ... statements in a file myImports.scala and then call
scala-cli repl imports.scala ─╯
Starting compilation server
Compiling project (Scala 3.4.1, JVM (21))
[warn] ./imports.scala:6:1
[warn] No class, trait or object is defined in the compilation unit.
[warn] The incremental compiler cannot record the dependency information in such case.
[warn] Some errors like unused import referring to a non-existent class might not be reported.
Compiled project (Scala 3.4.1, JVM (21))
Welcome to Scala 3.4.1 (21.0.2, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
Then the imported stuff doesn’t work.
If you add some dummy object into the file: object Spam then those messages go away, but the imported stuff still does not work.
So, with scala-cli repl myfile.scala you can only use the stuff you define yourself in the file, can’t directly access the imports. (Maybe this is a REPL limitation? Scala-cli is not in charge of the REPL.)
The stuff in myImports.scala is compiled an available to the repl. Its just that import is a local thing only visible in the file it is used in. You could use export which makes things visibile outside the file, but that does not work on open-ended packages, but it will work on e.g. exporting the stuff in an object.
(There is a long thread here in Contributors discussing many aspects and details of this. The reason why whole-package exports is not made available is that it will make incremental compilation slow in tools using it as all the files needs to be checked because packages are “open-ended”, i.e. any file can add stuff to a package.)
//> using scala 3.4
export Stuff.*
object Stuff:
def hello = println(msg)
val msg = "hello"
Start repl with scala-cli repl test.scala and you have the stuff in Stuff available without import:
Welcome to Scala 3.4.1 (17.0.8.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala> hello
hello
scala> msg
val res0: String = hello