I have a Scala CLI project with all its code under the project root. the projects defines the package org.mydomain.mygroup.myproj
.
After I drop into a REPL from the project root via
scala-cli repl . -O -explain
I can do
import org.mydomain.mygroup.myproj._
and all definitions of the project are available. So far so good.
Now I try to put this import statement into the scala-cli
command line:
scala-cli repl . -O -explain -e "import org.mydomain.mygroup.myproj._"
This executes without complain, but the definitions of my project are not available in the REPL.
What goes wrong? How can I write the command that the definitions are available?
ps: I also tried to put the import into a init.scala
file and call it like this
scala-cli repl . -O -explain init.scala
However I get the same result: No complaint, but no definitions available either.
This is a requested feature:
opened 09:18AM - 13 Feb 24 UTC
enhancement
UX
REPL
## **Is your feature request related to a problem? Please describe.**
It is t… edious and annoying to have to retype `import allthethings.*` everytime I start the repl with `scala-cli --repl .`
## **Describe the solution you'd like**
I would like a way to state what initial imports I want to directly access in all repl sessions of the current build. It should be able to do this both with a using-directive and a command-line option.
## **Describe alternatives you've considered**
A more general solution is to provide the corresponding feature of sbt's `console / initialCommands` which would be more powerful. In sbt you write for example:
```scala
console / initialCommands := "import mypack.stuff.*, otherpack.morestuff.*"
```
I have tried using the scala-cli option `--scala-snippet` but that does not work with scala-cli version 1.1.2 as it gives this warning and ignores the import:
```scala
$ scala-cli repl . --scala-snippet "import reqt.*"
Starting compilation server
Compiling project (Scala 3.3.1, JVM (17))
[warn] ./.scala-build/reqT-solver_6554fc5f28-77219591e6/src_generated/main/scala-snippet.scala:1: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.
[warn] import reqt.*
[warn] ^^^^^^^^^^^^^
```
## **Additional context**
This has been discussed in both #604 and #2185 - the latter has a wider scope.
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.
4 Likes
Russ
December 24, 2024, 12:01am
5
Back in Scala 2, you could start the REPL with a -i command line option to automatically import something. I put that in a two-line bash script to use the REPL as a units-aware calculator based on my scalar class for physical units. For some unexplained reason, however, the -i option got lost in Scala 3.
I don’t think you would find the explained reason very satisfying.
Russ
December 24, 2024, 5:56am
7
It couldn’t be any less satisfying than not knowing why.
I found a workaround using sbt, but it requires access to sbt, whereas the -i option does not.