From b4b7bf39a3b72d65589490fc94e9175da2b032d7 Mon Sep 17 00:00:00 2001 From: Piotr Chabelski Date: Wed, 26 Nov 2025 09:46:26 +0100 Subject: [PATCH] Add examples for passing REPL options via using directive to the docs (#3975) * Add examples for passing REPL options via using directive to the docs * Apply suggestions from code review Co-authored-by: Tomasz Godzik --------- Co-authored-by: Tomasz Godzik --- website/docs/commands/repl.md | 73 ++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/website/docs/commands/repl.md b/website/docs/commands/repl.md index 2601d1808f..98df8b4675 100644 --- a/website/docs/commands/repl.md +++ b/website/docs/commands/repl.md @@ -119,20 +119,47 @@ scala> +- via a using directive, treating them as Scala compiler options: + + + +```scala compile title=repl-options.scala +//> using toolkit default +//> using options --repl-init-script "import os.*; println(pwd)" +``` + +```bash ignore +scala repl repl-options.scala +``` + +``` +/current/directory/path +Welcome to Scala 3.7.4 (17, Java OpenJDK 64-Bit Server VM). +Type in expressions for evaluation. Or try :help. + +scala> pwd +val res0: os.Path = /current/directory/path +scala> :quit +``` + + + - directly, as a Scala CLI option (do note that newly added options from an RC version or a snapshot may not be supported this way just yet): ```bash ignore -scala repl -S 3.6.4-RC1 --repl-init-script 'println("Hello")' +scala repl --repl-init-script 'println("Hello")' ``` ``` Hello Welcome to Scala 3.6.4-RC1 (23.0.1, Java OpenJDK 64-Bit Server VM). Type in expressions for evaluation. Or try :help. - -scala> + +scala> pwd +val res0: os.Path = /current/directory/path +scala> :quit ``` @@ -147,13 +174,13 @@ scala-cli repl --toolkit default ``` ```text -Welcome to Scala 3.3.1 (17, Java OpenJDK 64-Bit Server VM). +Welcome to Scala 3.7.4 (17, Java OpenJDK 64-Bit Server VM). Type in expressions for evaluation. Or try :help. -scala> import os._ +scala> import os.* scala> os.pwd -val res0: os.Path = /Users/yadukrishnan/test +val res0: os.Path = /current/directory/path scala> :quit ``` @@ -161,6 +188,40 @@ scala> :quit Since we started the repl with toolkit enabled, we can use the libraries included in the toolkit directly. In the above example, the `os-lib` library from the toolkit is used to print the current path. +### Running snippets directly via the REPL +It is possible to run code snippets via the REPL, with all the internal quirks of the REPL, rather than using standard runner execution. +While the difference is subtle and should generally be invisible to users, it is useful for investigating REPL behavior. +The key to doing that is to use the both `--repl-init-script` and `--repl-quit-after-init` options together. + + + + +```bash +scala repl --repl-init-script 'println(42)' --repl-quit-after-init # runs in the REPL and quits immediately +``` + +``` +42 +``` + +```bash +scala repl -e '//> using options --repl-init-script "println(42)" --repl-quit-after-init' # same, but via using directive +``` + +``` +42 +``` + +```bash +scala -e 'println(42)' # standard runner equivalent +``` + +``` +42 +``` + + + ## Inject code as JAR file in class path If your application inspects its class path, and requires only JAR files in it, use `--as-jar` to