Learning Kotlin: Hello World

NoteThe code being referenced. This is the 1st post in a multipart series. If you want to read more, see our series index.

So let's start with line one:

package i_introduction._0_Hello_World

In Kotlin, package works just like a package in Java or a namespace in .NET. There is no link between the package name and the filesystem.


Next up are imports:

import util.TODO
import util.doc0

Imports work the same as import in Java or using in C#. Kotlin imports the following packages by default:


Next up is our first function definition (not defination):

fun todoTask0(): Nothing = TODO()

We use the fun keyword to state it is a function, followed by the function name todoTask0 and its parameters—in this case, none. The function returns Nothing, though this isn’t strictly required, as the compiler can infer the return type. This is a single-statement function, so it ends with an equal sign (=).

The next function is slightly different:

fun task0(): String {
    return todoTask0()
}

This function returns a String and is not a single-statement function.


So how do we solve this Koan?

fun task0(): String {
    return "OK"
}