Load a file on startup into Scala 3 REPL

Is there a way to load a scala file into the Scala 3 REPL on startup?

I know after startup I can say :l myfile.scala, but when I start the Scala REPL on the command line:

$ scala --explain

I often know already that I want to load myfile.scala.

1 Like

You can do so with scala-cli. It lets you start a REPL with something loaded:

scala-cli repl myFile.scala

The command is long to type, but you can make an alias for it in your Terminal sessions. Like:

alias myrepl="scala-cli repl /path/to/myFile.scala"
1 Like

Scala 2 had the -i option for loading a file at REPL startup, but that apparently got lost in Scala 3. I’d like to see it come back.

1 Like

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.

Related feature request:

If you have ideas to add on its use, feel free to comment on the issue.

2 Likes

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 myImports.scala

?

If you do that, it says this:

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.)

I think we still have to wait on the issue @bjornregnell linked above. I added this use case to the issue.

2 Likes

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.

1 Like

(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.)

1 Like

An example in file test.scala:

//> 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
1 Like

Aha! export is very useful! Put everything in an object then export, brilliant!