Ballerina Shell — A REPL for Ballerina
Ballerina is an open-source general-purpose programming language and platform for cloud-era application programmers. To learn more about Ballerina, visit https://ballerina.io
If you are a Ballerina developer or are a person wanting to learn Ballerina, one of the problems you may have is “How can I get started?” or “How can I explore the Language?”. Ballerina-By-Example is a great resource, but you might be overwhelmed by the information there or need a better way to learn the language by trial-and-error. For Python, we have Python Shell, a great way to explore the language. For Java, we have Jshell, an easy way to prototype Java code. Starting from Ballerina Swan Lake, Ballerina will also get a REPL, Ballerina Shell 🎉.
Shells (aka REPLs or Read-Eval-Print-Loops) are popular and highly interactive tools that allow developers to quickly evaluate complex expressions and see their results without the hassle of setting up a file. Using a Shell, beginners can easily get into the language while experienced developers can check-out new language/library features.
REPLs are very common in many popular scripting languages such as Python or JavaScript. These are extremely useful as great learning tools as well as great debugging tools.
Ballerina Shell Basics
Launching Ballerina Shell is as simple as running bal shell
in your preferred terminal. Within the shell, you can quickly evaluate any valid Ballerina expression. You can even define variables, functions, classes, and even do imports.
Note: Variable declarations in the REPL are module-level global declarations and you should always provide the initial value for global variables when you declare them.
One of the key features in a REPL is to try code snippets and immediately see the feedback, be it the result, or an error. Any error that occurred within Ballerina Shell will be shown to you immediately, and you can easily continue your work.
At any point, if you feel the need to see the declarations you have done, you can simply use built-in commands; /dclns
, /vars
or /imports
. You can reset the session at any-time using the command /reset
. Use /help
to see all the available built-in commands.
Following is a sample session of trying out theballerina/http
module. You can see how much easier it is to try out new modules and explore the language via the REPL.
Summary
Ballerina Swan Lake brings a great tool, Ballerina Shell, which I hope every Ballerina developer will use to increase their productivity and as an exploration tool to the Ballerina Language. This is an experimental tool, still in its early development, so there can be drastic changes in the coming months. There will be more features coming soon and your valuable feedback is highly required to make this a better tool. If you find an issue, please don't be hesitant to file an issue.
In the next blog post, I will be looking into the implementation of Ballerina Shell and how this all works internally.