How to Use the Kotlin Playground

How to Use the Kotlin Playground

As a modern Android developer, sometimes I find myself with the need of wanting to run some small snippet of code. For these kinds of situations, the lesser-known Kotlin Playground is a great option to try out code on the fly.

What is the Kotlin Playground?

Initially published about 5 years ago, the Kotlin Playground is an online lightweight compiler that allows Kotlin programmers to run code directly in the browser using different versions of Kotlin, and even target different platforms.

Additionally, the Kotlin Playground gives developers the opportunity to both share their code with other people, as well as embed a small version of the compiler into websites, and particularly into Medium.

How does it work?

Using the Kotlin standard libraries only, the Kotlin Playground can run mildly complex blocks of code directly in your browser without much setup, and without the burdensome matter of ramping up and working with a chunky IDE.

Simply, access https://play.kotlinlang.org/ and you can start playing around almost immediately!

/**
 * You can edit, run, and share this code.
 * play.kotlinlang.org
 */
fun main() {
    println("Hello Medium!")
}

As soon as the compiler is filled with the code you want to execute, find the “Run” button on the top-right corner of the UI, and wait for the result on the bottom panel. If there are any errors with the execution, the bottom panel will simply hide itself.

Changing the Settings

There are two main editable settings inside the Kotlin Playground. Starting at the top-left side of the screen, underneath the big Kotlin banner, you can find the Kotlin version that the Playground is currently running on. Simply clicking and choosing a different version will make sure your code is compiled and executed in a different Kotlin version.

On the right-hand side of the Kotlin version, you can find the Target Platform that your code would be executed on. Similarly to the Kotlin version, a couple of clicks will expand the different Target Platform options and change it in order to make your code run differently.

Before moving on, let’s quickly go over the current options present for different Target Platforms:

  • JVM: Java Virtual Machine to run Kotlin in the default way

  • JS: JavaScript for server-side development; e.g., Node.js, JQuery, React.

  • JS IR: JavaScript Intermediate Representation is a new way for the Kotlin compiler to parse Kotlin source files

  • JUNIT: JUnit to run test code using regular JUnit annotations like @Test

  • CANVAS: Introduced by HTML5, this platform allows you to draw graphics using JavaScript

Last but not least, there’s also a way of adding an argument into the program execution.

In the example above, we’re adding the argument Medium. The main function of the code requires a small refactor in order to be able to receive and process arguments. If we run this program, we would get the following output

Hello, Medium!
Sharing and Embedding

The Kotlin Playground allows for a few different methods for sharing the code that is currently present in its console.

First, you can simply click the “Copy link” button in order to quickly share a link to the website with the current contents of the console.

The following panel will demonstrate how it looks when we paste the copied link into Medium.

Contents of link: https://pl.kotl.in/Eg7HMM2jR

Moreover, the Kotlin Playground also allows us to share our code by embedding it into a website, or any other method that supports embedding.

There are two main ways of embedding code from the Kotlin Playground. The first way is the default setting you see after clicking the “Share code” button, and it is meant to be added directly to HTML code.

The second way to get the content that we want to embed, is the alternative setting inside the sharing panel which is called “Medium.” This method will simply allow you to copy an address that you can then paste directly into (you guessed it) Medium or any other websites that allow for the Markup language or something of the sort.

In this last example we’ll show how it looks when we paste the embedding link into Medium directly, but should work for other tools using Markup too.

fun main(args: Array<String>) {
    println(String.format("Thank you for reading, %s", args[0]))
}

Contents of link: https://pl.kotl.in/WXIS40nvn

It should be noted that both ways of adding a preview — either by copying the link and pasting it or by embedding it directly — allow the reader to execute and edit the code directly in the panels that are shown.

Nevertheless, copying the link seems to truncate the view a lot more, and requires accepting the cookies agreement as a regular website would. Differently, the embedded playground instance not only gives more freedom to the sharer on how it looks, but it sports a more dynamic view that doesn’t require any cookie agreements and resizes itself as required.

How does it compare to Swift Playgrounds?

If there are any iOS developers still reading at this point, I know what you’re thinking: the Swift Playgrounds app has been out for some years now. I can acknowledge this, as I remember using it myself back in 2017!

The Kotlin Playground is not as powerful, or featureful as the Swift Playgrounds is nowadays. But the biggest advantage that the Kotlin Playground has over its adversary is that Kotlin can be run and compiled directly in the browser.

Conclusion

All in all, the Kotlin Playground is fun and useful at the same time. It allows us Kotlin programmers to run small pieces of code on the fly, without the need to start up an expensive IDE. Additionally, it’s an elegant way of sharing code around, or you know, adding it into a Medium article!

I hope this article served as a simple introduction to this powerful and practical tool, and that you can too find its use as part of your development process. Happy coding!

Mansi Vaghela LinkedIn Twitter Github Youtube