Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature initImportREPL: allow initial imports to be automatically available in repl session #2732

Open
bjornregnell opened this issue Feb 13, 2024 · 3 comments
Labels
enhancement New feature or request REPL Issues tied with the repl sub-command. UX

Comments

@bjornregnell
Copy link
Contributor

bjornregnell commented Feb 13, 2024

Is your feature request related to a problem? Please describe.

It is tedious 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:

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

@spamegg1
Copy link
Contributor

spamegg1 commented Apr 26, 2024

Yes, a use case would be to have a file like this: for example

// this is imports.scala
//> using toolkit default
//> using dep org.apache.commons:commons-numbers-combinatorics:1.1
//> using dep org.scala-lang.modules::scala-parallel-collections:1.0.4
//> using dep org.scala-graph:graph-core_2.13:2.0.1
// more dependencies if needed

import collection.parallel.CollectionConverters.*
import scala.util.boundary, boundary.break
import org.apache.commons.numbers.combinatorics.{BinomialCoefficient, Factorial}
import scalax.collection.generic.AbstractDiEdge
// more imports if needed

then to start the repl with:

$ scala-cli repl imports.scala

and have the imports accessible in the REPL, for example:

scala> BinomialCoefficient.value(10, 2)
val res0: Long = 45

Although maybe this is not possible due to REPL's own limitations?
How does scala-cli repl --toolkit default do it? When we use that, os.??? is available immediately.

@Gedochao
Copy link
Contributor

How does scala-cli repl --toolkit default do it? When we use that, os.??? is available immediately.

If you invoke any API while prefixing the full package (as os.??? is the full package path), then no imports are necessary.

Although maybe this is not possible due to REPL's own limitations?

@spamegg1 AFAIK the REPL currently does not have the API to deliver this, as well as a couple of other requested features.
However, improving this situation actually is on the 2024 Roadmap for Scala 3. So things might change soon.

@bjornregnell
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request REPL Issues tied with the repl sub-command. UX
Projects
None yet
Development

No branches or pull requests

3 participants