To run Scala from the command-line, download the binaries and unpack the archive. Start the Scala interpreter (aka the “REPL”) by launching scala from where it was unarchived. Start the Scala compiler by launching scalac from where it was unarchived.
The Scala REPL (Read-Eval-Print-Loop) is a development tool to interpret fragments of Scala code. It doesn't require too much setup or infrastructure and it is going to be essential during your learning journey. Using the REPL, you'll be able to play and experiment with the language by typing and evaluating code.
The scala command will execute a source script by wrapping it in a template and then compiling and executing the resulting program. In interactive mode, the REPL reads expressions at the prompt, wraps them in an executable template, and then compiles and executes the result.
type :load /path/to/file
in Scala REPL.
You can get complete list of available commands by typing :help
On occasions, :paste
might be your better friend (than :load
). Here is an example on how to use :paste.
scala> :paste
// Entering paste mode (ctrl-D to finish)
if (true)
print("that was true")
else
print("false")
[Ctrl-D]
// Exiting paste mode, now interpreting.
that was true
One can also use :paste
to load a file using following command :paste [path]
scala> :paste ~/Desktop/repl_seeder.scala
Pasting file ~/Desktop/repl_seeder.scala...
defined object test1
scala> test1.main(Str)
my first scala program
Just reminder, put the complete path. I found problem in Linux by doing like this:
:load ~/fileName.scala
to get rid of error "That file does not exist" I did
:load /complete/path/fileName.scala
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With