As shown in the image, I was just reminded that you can run
javap inside the Scala REPL. The REPL help command shows some other things you can do:
scala> :help All commands can be abbreviated, e.g., :he instead of :help. :edit <id>|<line> edit history :help [command] print this summary or command-specific help :history [num] show the history (optional num is commands to show) :h? <string> search the history :imports [name name ...] show import history, identifying sources of names :implicits [-v] show the implicits in scope :javap <path|class> disassemble a file or class name :line <id>|<line> place line(s) at the end of history :load <path> interpret lines in a file :paste [-raw] [path] enter paste mode or paste a file :power enable power user mode :quit exit the interpreter :replay [options] reset the repl and replay all previous commands :require <path> add a jar to the classpath :reset [options] reset the repl to its initial state, forgetting all session entries :save <path> save replayable session to a file :sh <command line> run a shell command (result is implicitly => List[String]) :settings <options> update compiler options, if possible; see reset :silent disable/enable automatic printing of results :type [-v] <expr> display the type of an expression without evaluating it :kind [-v] <type> display the kind of a type. see also :help kind :warnings show the suppressed warnings from the most recent line which had any
Answer: Use the
consoleQuick commands inside the SBT shell:
consoleto start a REPL session from inside SBT. This (a) compiles your Scala project and then (b) starts a REPL session.
consoleQuickif you don’t want to compile your project before starting a REPL session inside of SBT.
console command process looks like this:
> sbt [info] Loading project definition from /Users/al/Projects/Cats101/project [info] Set current project to Cats101 (in build file:/Users/al/Projects/Cats101/) > console [info] Starting scala interpreter... [info] Welcome to Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131). Type in expressions for evaluation. Or try :help. scala> _
If you wanted to see how to start a Scala REPL session from inside SBT, I hope this helps.
This image shows how to use color syntax highlighting in the Scala REPL. In short, just start the REPL like this:
$ scala -Dscala.color
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.4, “How to run a shell command from the Scala REPL.”
You want to be able to run a shell command from within the Scala REPL, such as listing the files in the current directory.
Run the command using the
:sh REPL command, then print the output. The following example shows how to run the Unix
ls -al command from within the REPL, and then show the results of the command:
A cool feature of the Scala REPL is that you can reset/clear a REPL session. To do so, just issue the
:reset command, like this:
scala> :reset Resetting interpreter state. Forgetting this session history:
Assuming that you already have at least a little history in your REPL session, the
:reset command will show you everything that it dumps, so the full output looks more like this:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.2, “How to compile, run, and package a Scala project with SBT.”Back to top
You want to use SBT to compile and run a Scala project, and package the project as a JAR file.Back to top
Create a directory layout to match what SBT expects, then run
sbt compile to compile your project,
sbt run to run your project, and
sbt package to package your project as a JAR file.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 14.3, “How to add Jar files and classes to the REPL Classpath.”
You want to add individual classes or one or more JAR files to the REPL classpath so you can use them in a REPL session.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.2, “How to paste and load blocks of code in the Scala REPL.”
You want to experiment with some code in the Scala REPL, and typing it in or trying to paste it into the REPL won’t work.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.1, “How to get started with the Scala REPL.”Back to top
You want to get started using the Scala REPL, including understanding some of its basic features, such as tab completion, starting the REPL with different options, and dealing with errors.Back to top
To start the Scala REPL, type
scala at your operating system command line: