DEV Community

Cover image for Trying out code

Trying out code

tkuenneth profile image Thomas Kuenneth ・4 min read

Being a fan of polyglot programming means switching languages frequently. Re-acquainting oneself with a particular one involves doing simple programming excercises. Of course, we can open our favorite IDE and setup a small project. Even though this is just a matter of seconds, there must be a simpler way, right?


Why not train your language skills in a REPL? REPL means Read-Evaluate-Print-Loop, which describes pretty well how such tools work. You enter a command or an expression, which is then evaluated. If the code is valid its result is printed. Otherwise you will see an error message describing the problem. Usually REPLs accumulate state, so once a variable has been defined you can inspect and alter its value. This is ideal for learning language behavior, for example how scope and visibility work.


If you are on a Mac and have both Xcode and the Xcode commandline tools installed, simply open the Terminal and type swift. It's in /usr/bin. As you can see in the following screenshot, you can enter any expression and command you would like to try out.

Swift REPL

Entries are recorded in a history file and persisted through sessions (~/.lldb/lldb-repl-history). Hence, correcting a mistake is as easy as pressing the arrow-up-key. To quit the REPL, enter :q. :help prints a list of commands. For further information, take a look at the documentation provided by Apple

Another way to try out Swift is using an Xcode playground. Once your code snippet has been executed you can inspect values. Changes do not take effect immediately, you need to replay the altered line. Static code analysis is performed during input, so you get appropriate error messages if your code is invalid.

An Xcode playground

The concept behind playgrounds is present in several IDEs. For example, Eclipse has been offering so-called scrapbook pages to try out Java code for many years. IntelliJ has scratch files. You will be seeing them shortly.


Recent versions of the Java Development Kit (actually, starting with Java 9) ship with a REPL called JShell. The basic usage is similar to the Swift REPL. For example, you can browse through the list of previously typed expressions using the up/down arrow keys. Control-a moves the cursor to the beginning, Control-e to the end of the current line. Control-k removes (kills) all characters on the right hand side of the cursor.

Java's JShell

/help prints a list of commands. /exit quits JShell. To get help for a specific command, use something like /help save.

What's really cool about JShell is that you do not need to provide a class that hosts methods or members. You can concentrate on your commands and expressions. Of course, class definitions are fully supported. The following screenshot shows how you enter a class and later instantiate an object.

Entering and instantiating classes and objects

JShell can save and restore sessions. Such files consist of so-called snippets, the pieces of Java code you entered (for example statements, variables, methods and class definitions). /list shwos the list of snippets. /edit opens a basic editor. /drop removes a snippet.


The easiest way to try out the Dart programming language is through the so-called DartPad. Its main difference to a traditional REPL is that input is not evaluated until the Run button is pressed. So, there is no heap or stack that gets filled with the results of evaluated expressions. And there is no history. You do get results from a static code analysis while you type, though. A section of the screen provides documentation to library functions. Finally, a list of samples helps you get started with the Dart syntax.



JetBrains offers an online playground for its programming language, too. You enter your Kotlin program or choose from an extensive list of examples. After hitting the green Play button the code is executed. Static code analysis takes place during input. So issues are immediately visible in the Problems view.

Online editor at

Just like DartPad, this online editor is not a classic REPL. There is only state during the execution of the program. And you must provide an entry point, in both cases a main() function.

As I have already mentioned, JetBrains' IDE IntelliJ offers something similar to Xcode playgrounds. Scratch files are available for quite a few languages. The following screenshot shows one for Kotlin.

IntelliJ scratch files

A so-called interactive mode runs the scratch file if you have stopped typing for two seconds. A checkbox called Use REPL executes only new expressions which have been added to the end of the scratch file.


Playgrounds and REPLs are ideal for learning the syntax and features of a programming language, because they free you from the need to setup projects and write boilerplate code just to get things going. Hence, they are an important tool for the polyglot programmer. Certainly, other languages have their REPLs, too. A future post might be discussing them. In the meantime, please feel free to share your thoughts and comments regarding the use of REPLs.

Discussion (0)

Editor guide