Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin or The Quest For Java.Next

Ladislav Thon
February 08, 2014
170

Kotlin or The Quest For Java.Next

A lot of people in the Java community has been long longing for some kind of a "Java.Next" language, a language that will be modern yet keep the design spirit. There is a lot of more or less suitable candidates -- let's take a brief look at some of them and then examine Kotlin in more detail. Kotlin comes from JetBrains, a company with more than 10 years of experience developing industry-leading developer tools and therefore pretty unique perspective on language design. We'll take a look at some of the more interesting features: null-safety and other nice little things in the type system, language-supported delegation, lambdas, extension methods and DSL support. Few words about Java interop too.

Ladislav Thon

February 08, 2014
Tweet

Transcript

  1. Classes package _02classes class Person3(val name: String) fun main(args: Array<String>)

    { val person = Person3("Ladislav Thon") println(person) }
  2. Classes package _02classes data class Person4(val name: String) fun main(args:

    Array<String>) { val person = Person4("Ladislav Thon") println(person) }
  3. Traits package _03traits class Fubar : Foo, Bar { override

    fun foo() = "foo" fun fubar() { println("${foo()}-${bar()}") } }
  4. Multiple Inheritance? • single inheritance + interfaces ◦ Java <=

    7, C# • stateless traits ◦ Kotlin, Ceylon, Java 8 (kind of…) • stateful traits ◦ PHP >= 5.4 (I think…) • mixins ◦ Scala, Dart • full multiple inheritance ◦ C++
  5. Null Safety & Smart Casts package _04nullSafety fun main(args: Array<String>)

    { val maybeAnswer: Int? = ... if (maybeAnswer != null) { val answer: Int = maybeAnswer } }
  6. Null Safety & Safe Navigation & Elvis package _04nullSafety fun

    main(args: Array<String>) { val answer: Int? = … val doubleAnswer = answer?.let { it * 2 } answer?.times { println("Hi!") } val question = answer ?: 42 }
  7. Generics package _06generics trait Producer<out T> { fun produce(): T

    } trait Consumer<in T>{ fun consume(t: T) }
  8. Generics package _06generics trait Holder<T> { fun produce(): T fun

    consume(t: T) } fun use(h: Holder<out String>) { h.produce() }
  9. Extension Methods package _07extensions fun String.pirateStyle() = "Arrr! ${this.capitalize()}" fun

    main(args: Array<String>) { println("hello, world".pirateStyle()) }
  10. Lambdas package _08lambdas fun main(args: Array<String>) { val nums =

    arrayListOf(1, 2, 3, 4, 5, 6, 7) .filter { it % 2 == 0 } .flatMap { listOf(it, it * 2, it * 3) } .takeWhile { it < 10 } println(nums) }
  11. Lambdas package _08lambdas fun Lock.locked<T>(compute: () -> T): T {

    this.lock() try { return compute() } finally { this.unlock() } }
  12. Delegation package _09delegation class MyList<T>(val l: List<T>) : List<T> by

    l fun main(args: Array<String>) { val list = MyList<String>(listOf("a, b, c")) list.forEach { println(it) } }
  13. Delegation package _09delegation class LazyString { val value: String by

    Delegates.lazy { println("heavy computation") "the string!" } }
  14. Builders & DSL support package _10builders fun data(build: Data.() ->

    Unit): Data { val result = Data() result.build() return result }