A First Look at Kotlin

Next on the things that keep cropping up in my searches is Kotlin. This appears to be a Java derivative that can compile to a few different targets, one of which is Javascript, so I thought this might be a good idea.


It appears that Kotlin comes free with IntelliJ IDEA which is an editor I use occasionally at work when I need to work with Java, so at least this is familiar territory. It’s not a tiny download, but there is a free Community Edition that seems perfectly good for my needs. The installation was simple…and even if I don’t go down the Kotlin route I would definitely see myself using this for other Java projects.


Next I had a look at the tutorials and started to look at the one related to Javascript output. This, I thought, should be easy, but I was not pleasantly surprised. The tutorial for IDEA is pretty simple…because it doesn’t really do anything. It tells you how to make a project, but doesn’t tell you how to build it or run it. Well, that’s not strictly true – it tells you how to debug it, but personally I’d rather not have to leap through all of the hoops of setting up a Chrome plugin just so I can look at the output of hello world.

Building was pretty obvious really, and there is a nice feature that I can look at the index.html file I added by clicking on the little chrome button that appears on the right hand side of the editor. I did get a bit confused as the HTML that is essential to the project is not part of the final output and includes relative paths to the js output (“out/production/Witch/lib/Witch.js”) – this is definitely not going to be the final HTML.

So once I’d got these pieces together I had my tutorial completed and I have a web page that shows a string in the Javascript console.


The problem is, after this slightly rocky start I was a bit lost. I look at the main language tutorial and all I get is a very simple page saying I should do some Koans. This was also confusing as my knowledge of Zen Buddhism is limited, and certainly wasn’t something I’d associate with learning a programming language. But to be honest, it’s not a bad idea – the koans are a method to learn by example.

The small issue I had with the Koans was that usually I had no idea what the answer would be as I don’t know the language at all. It’s a bit like if I was learning french and was just asked out of the blue “What’s the french for pomegranate?” – how would I know if I haven’t encountered that yet? So usually I just looked at the answer and that told me what they were trying to teach. I think it sort of misses the point.

The larger issue I had with the Koans was that I didn’t see the point. They were teaching me how to translate Java into Kotlin, but I’m more concerned about how to I get started. What’s the syntax of a for loop? How do I declare functions or variables? Knowing how I can create a new Iterator seems like a far more advanced topic than I needed at this stage.

Javascript output

After doing a few koans, I realised I was wasting my time as it’s not teaching me what I need to do now, so I went back to the javascript tutorial looking for how I could, say, output a message to a web page. There is a tutorial page about DOM interaction, which stated I could simply use document to get at the document, similar to in raw javascript. Great! Except…that doesn’t work just like that.

Firstly I had a wonderfully useful message “You have two compiler errors”, but no idea as to what those errors were! It turns out that I needed to update the Kotlin plugin in my newly downloaded IDEA. Once I’d fixed that the errors turned a little more useful, although it took some cajoling to get it to give me a hint about what I needed to import (once I’d got kotlin.browser.document, that was the key).

So finally I had a small javascript page that produces some text when I load it.

import org.w3c.dom.HTMLDivElement
import kotlin.browser.document

fun main(args: Array<String>) {
    val main = document.getElementById("main") as HTMLDivElement
    main.innerHTML = "hello"

This has a simple HTML file:

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
<div id="main"></div> 
<script type="text/javascript" src="out/production/Witch/lib/kotlin.js"></script>
<script type="text/javascript" src="out/production/Witch/Witch.js"></script>


The Kotlin tutorial is definitely not particularly simple or easy, and I don’t feel like I’m ready to conquer the world yet with the language. But I do have a starting point, so I’m going to have a further look. I am a little worried that this doesn’t seem too much more than a way to write javascript in a different language, but perhaps that is just what I’m going to end up with.

The next step here is to try to get a game loop going and see how difficult it will be to get back to where I was with Elm.

Leave a Reply

Your email address will not be published. Required fields are marked *