Skip to content

Commit 17915ac

Browse files
authored
Merge pull request #3978 from VirtusLab/stable
Back port of documentation changes to main - #3975
2 parents 6a64c14 + b4b7bf3 commit 17915ac

1 file changed

Lines changed: 67 additions & 6 deletions

File tree

website/docs/commands/repl.md

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,47 @@ scala>
119119

120120
</ChainedSnippets>
121121

122+
- via a using directive, treating them as Scala compiler options:
123+
124+
<ChainedSnippets>
125+
126+
```scala compile title=repl-options.scala
127+
//> using toolkit default
128+
//> using options --repl-init-script "import os.*; println(pwd)"
129+
```
130+
131+
```bash ignore
132+
scala repl repl-options.scala
133+
```
134+
135+
```
136+
/current/directory/path
137+
Welcome to Scala 3.7.4 (17, Java OpenJDK 64-Bit Server VM).
138+
Type in expressions for evaluation. Or try :help.
139+
140+
scala> pwd
141+
val res0: os.Path = /current/directory/path
142+
scala> :quit
143+
```
144+
145+
</ChainedSnippets>
146+
122147
- 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):
123148

124149
<ChainedSnippets>
125150

126151
```bash ignore
127-
scala repl -S 3.6.4-RC1 --repl-init-script 'println("Hello")'
152+
scala repl --repl-init-script 'println("Hello")'
128153
```
129154

130155
```
131156
Hello
132157
Welcome to Scala 3.6.4-RC1 (23.0.1, Java OpenJDK 64-Bit Server VM).
133158
Type in expressions for evaluation. Or try :help.
134-
135-
scala>
159+
160+
scala> pwd
161+
val res0: os.Path = /current/directory/path
162+
scala> :quit
136163
```
137164

138165
</ChainedSnippets>
@@ -147,20 +174,54 @@ scala-cli repl --toolkit default
147174
```
148175

149176
```text
150-
Welcome to Scala 3.3.1 (17, Java OpenJDK 64-Bit Server VM).
177+
Welcome to Scala 3.7.4 (17, Java OpenJDK 64-Bit Server VM).
151178
Type in expressions for evaluation. Or try :help.
152179
153-
scala> import os._
180+
scala> import os.*
154181
155182
scala> os.pwd
156-
val res0: os.Path = /Users/yadukrishnan/test
183+
val res0: os.Path = /current/directory/path
157184
scala> :quit
158185
159186
```
160187
</ChainedSnippets>
161188

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

191+
### Running snippets directly via the REPL
192+
It is possible to run code snippets via the REPL, with all the internal quirks of the REPL, rather than using standard runner execution.
193+
While the difference is subtle and should generally be invisible to users, it is useful for investigating REPL behavior.
194+
The key to doing that is to use the both `--repl-init-script` and `--repl-quit-after-init` options together.
195+
196+
<ChainedSnippets>
197+
198+
199+
```bash
200+
scala repl --repl-init-script 'println(42)' --repl-quit-after-init # runs in the REPL and quits immediately
201+
```
202+
203+
```
204+
42
205+
```
206+
207+
```bash
208+
scala repl -e '//> using options --repl-init-script "println(42)" --repl-quit-after-init' # same, but via using directive
209+
```
210+
211+
```
212+
42
213+
```
214+
215+
```bash
216+
scala -e 'println(42)' # standard runner equivalent
217+
```
218+
219+
```
220+
42
221+
```
222+
223+
</ChainedSnippets>
224+
164225
## Inject code as JAR file in class path
165226

166227
If your application inspects its class path, and requires only JAR files in it, use `--as-jar` to

0 commit comments

Comments
 (0)