Safe and sound code with kotlin

Following my first talk about writing safe code in kotin at London Kotlin Meetup #2 I recently had one in Saint-Petersburg. It was organised by Spb Google Developers Group, namely by Oleg Makarov. Event took place in the most appropriate place – place where kotlin was born – Jetbrains “Universe” Office at Vasilyevskiy island (it’s not kotlin island as someone might think :D).

Topic of that talk was same as in London but reflected by received feedback plus some extra things.

Here is slides

All examples can be found at my repo

The extra example was with typesafe units math. With kotlin it’s possible to work with different dimensions in very safe manner, so you won’t subtract liters from wats. In my basic example I work with distance, time and speed:

fun main(args: Array<String>) {
    println(10.asSeconds + 5.asMinutes)
    // 310.0s

    val speed = 10.asKilometers / 5.asHours
    val speedInMPerSecond = / Seconds)
    println("$speed = $speedInMPerSecond")
    // 2.0km/h = 0.556m/s

    val speedInMPerHour = / Hours)
    println("$speed = $speedInMPerHour")
    // 2.0km/h = 1.2427454732996135mi/h

    val `50mph` = 50.asMiles / 1.asHours
    println("$`50mph` = ${`50mph`.to(Kilometers / Hours)}")
    // 50.0mi/h = 80.467km/h

From km/h to mi/h – easily! Implementation might seem difficult to understand for someone isn’t familiar with kotlin syntax alot, so I broke up it to step-by-step instructions

According to there were ~100 people while I’d say ~80. After talk I was asked a lot of questions starting from presentation’s topics ending up with testing, frameworks and some hate of php and js of course.

And yeah, I already know what is the next topic I want to present :)