You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
split, fix and cleanup predef and runBefore handling (#176)
Prior to 0.1.92 we concatenated and executed all predef code. That was
very simple but had quite
a few downsides, including incorrect line number reporting and some edge
cases when handling complex
compilation units, e.g. ordering of imports etc.
#157 changed this
behaviour to instead compile
the given files and add the results to the classpath. At the same time
we unintendedly broke
the ability to execute code on startup of the repl - that's reintroduced
as a new parameter
`--runBefore` now.
-[Benefits over / comparison with](#benefits-over--comparison-with)
25
24
*[Regular Scala REPL](#regular-scala-repl)
26
25
*[Ammonite](#ammonite)
27
26
*[scala-cli](#scala-cli)
28
27
-[Prerequisite for all of the below: run `sbt stage` or download the latest release](#prerequisite-for-all-of-the-below-run-sbt-stage-or-download-the-latest-release)
29
28
-[REPL](#repl)
30
29
*[run with defaults](#run-with-defaults)
31
-
*[customize prompt, greeting and exit code](#customize-prompt-greeting-and-exit-code)
32
-
*[execute some predef code](#execute-some-predef-code)
30
+
*[execute code at the start with `--runBefore`](#execute-code-at-the-start-with---runbefore)
31
+
*[`--predef`: code that is compiled but not executed](#--predef-code-that-is-compiled-but-not-executed)
33
32
*[Operators: Redirect to file, pipe to external command](#operators-redirect-to-file-pipe-to-external-command)
34
33
*[Add dependencies with maven coordinates](#add-dependencies-with-maven-coordinates)
Additional source files that are compiled added to the classpath, but unlike `runBefore` not executed straight away can be provided via `--predef`.
107
125
```
108
126
echo 'def foo = 42' > foo.sc
109
127
@@ -112,6 +130,12 @@ scala> foo
112
130
val res0: Int = 42
113
131
```
114
132
133
+
You can specify this parameter multiple times (`--predef one.sc --predef two.sc`).
134
+
135
+
Why not use `runBefore` instead? For simple examples like the one above, you can do so. If it gets more complicated and you have multiple files referencing each others, `predef` allows you to treat it as one compilation unit, which isn't possible with `runBefore`. And as you add more code it's get's easier to manage in files rather than command line arguments.
136
+
137
+
Note that predef files may not contain toplevel statements like `println("foo")` - instead, these either belong into your main script (if you're executing one) and/or can be passed to the repl via `runBefore`.
138
+
115
139
### Operators: Redirect to file, pipe to external command
116
140
Inspired by unix shell redirection and pipe operators (`>`, `>>` and `|`) you can redirect output into files with `#>` (overrides existing file) and `#>>` (create or append to file), and use `#|` to pipe the output to a command, such as `less`:
117
141
```scala
@@ -274,6 +298,15 @@ $
274
298
```
275
299
Context: we'd prefer to cancel the long-running operation, but that's not so easy on the JVM.
0 commit comments