Learning Kotlin: Hello World

Submitted by Robert MacLean on Mon, 06/11/2018 - 09:00

More Information

So let's start with line one: package i_introduction._0_Hello_World

In Kotlin package is just the same as a package in Java or Namespaces in .NET. There is no link between package name and the filesystem.

Next up is imports:

  1. import util.TODO
  2. import util.doc0

Imports are the same as import in Java and Using in C#. Kotlin does import a number of packages by default:

  • kotlin.*
  • kotlin.annotation.*
  • kotlin.collections.*
  • kotlin.comparisons.* (since 1.1)
  • kotlin.io.*
  • kotlin.ranges.*
  • kotlin.sequences.*
  • kotlin.text.*

Next up is our first function defination fun todoTask0(): Nothing = ...

We use the fun keyword to state it is a function, followed by the function name todoTask0 and the parameters... in this case, that is empty.

The function returns Nothing though that isn't specifically 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 a little different

  1. fun task0(): String {
  2.     return todoTask0()
  3. }

The second function returns a String and is not a single statement function.

So how do we solve this Koan?

  1. fun task0(): String {
  2.     return "OK"
  3. }