Description
What needs to be done
We need a way to pass a code snippet or Scala source file that'd be run on REPL startup.
This is very similar to Ammonite
REPL's (https://github.com/com-lihaoyi/Ammonite or --amm
passed to Scala CLI) -c
/--code
option.
Why do we need this?
- it'd make REPL integrations testable on Scala CLI's side (things like Fix the REPL crashing when a dependency's classpath is called by a macro VirtusLab/scala-cli#3043 (comment))
- it'd enable working around package wildcard exports discussed in Support for initial commands in REPL VirtusLab/scala-cli#604
Requirements
I'd imagine we need:
- a way to pass a code snippet or source file to the REPL main class to be executed at REPL startup
- something like this:
java -Dscala.usejavacp=true -classpath (...) dotty.tools.repl.Main -classpath (...) --init-script 'println("Hello from the init script in REPL")' # Welcome to Scala 3.4.2 (17, Java OpenJDK 64-Bit Server VM). # Type in expressions for evaluation. Or try :help. # # Hello from the init script in REPL # scala>
- a way to skip the interactive mode and execute the script inside of the REPL, similar to what the
-e
flag does outside of the REPL- this would make testing integrations a lot easier
- Ammonite equivalent:
scala-cli repl --power --amm --ammonite-arg -c --ammonite-arg 'println("Hello from the init script in REPL")' # Compiling ~/Downloads/(console) # Hello from the init script in REPL
Context
Past discussions:
- Support for initial commands in REPL VirtusLab/scala-cli#604
- --interactive should not close the terminal after piping Scala code VirtusLab/scala-cli#2185
- Feature initImportREPL: allow initial imports to be automatically available in repl session VirtusLab/scala-cli#2732
- https://users.scala-lang.org/t/load-a-file-on-startup-into-scala-3-repl/9943/10