27
Kotlin: The Language of The Future For JVM? Leonardo Zanivan @ JavaOne Latin America 2016 [SES12196]

Kotlin: The Language of The Future For JVM?

Embed Size (px)

Citation preview

Kotlin: The Language of The Future For JVM?

Leonardo Zanivan @ JavaOne Latin America 2016 [SES12196]

Who am I?

➔ Leonardo Zanivan◆ Software Architect◆ OSS Contributor◆ GUJavaSC Member◆ JCP Member

First things first

➔ Why Kotlin?◆ Concise◆ Safe◆ Versatile◆ Interoperabile◆ Tooling

First things first

➔ History◆ Conceived in 2010◆ Language and compiler are open source (Apache 2)◆ To be a modern industry standard language◆ With static typing and a smooth migration path

First things first

➔ Why should I care?◆ Enterprise class support◆ Production ready◆ Java #1 language◆ Productivity

First things first

➔ Who is using?◆ GMC◆ Expedia◆ Prezi.com◆ Telegram◆ 99 Taxis◆ JetBrains (IDEA, YouTrack, TeamCity)

Easy to learn

➔ Extensive documentation◆ Online reference http://kotlinlang.org/docs◆ Online editor & training http://try.kotlinlang.org◆ Language documentation (PDF with 153 pages)◆ Books (Kotlin in Action and Kotlin for Android)◆ GitHub and StackOverflow activity

Easy to learn

➔ Fast startup◆ Intuitive, easy to read & write (opposed to Scala)◆ No force Functional or OOP styling

Easy to learn

Online editor & training http://try.kotlinlang.org

Easy to use

➔ Tooling support◆ IDEA Community & Ultimate◆ Android Studio◆ Eclipse◆ Maven / Ant / Gradle◆ NetBeans plugin (experimental)◆ Sonar plugin (experimental)

Multi target support

➔ Java➔ Android *➔ JavaScript (experimental)

Easy to migrate

➔ Mix Java and Kotlin source files in the same project➔ Java to Kotlin source conversion tool➔ Compatible with Java 6 bytecode (100% compatible)➔ Integrate with existing Java frameworks➔ No runtime overhead➔ No cost to adopt

KDoc

➔ Dokka is the documentation engine for Kotlin.

Feature: Null safety out-of-the-box

val givenName: String? = null

val len = givenName?.length //assign null

val len = givenName!!.length //assert null

Feature: Lean syntax (no get/set)

data class Book(var title: String, var author: Author)

val book = Book(“Kotlin at JavaOne”,”Leonardo”)

println(book.title)

Feature: Smart casts and type inference

if (node is Leaf) {

return node.symbol;

}

val myString = "Some text"

Functional programming

val numbers = arrayListOf(-42, 17, 13, -9, 12)

val nonNegative = numbers.filter { it >= 0 }

Easy singletons

object CardFactory {

fun getCard(): Card {

return Card();

}

}

Default arguments

class NutritionFacts(val foodName: String,

val calories: Int,

val protein: Int = 0,

val carbohydrates: Int = 0) {

}

Named arguments

val burger = NutritionFacts("Hamburger", calories = 541, protein = 14)

val rice = NutritionFacts("Rice", calories = 312, carbohydrates = 23)

Extension functions

fun Activity.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {

Toast.makeText(this, message, duration).show()

}

REPL

➔ Read-eval-print-loop

Other features

➔ Exceptions are all unchecked➔ Easy to use builder pattern (copy)➔ Operator overloading (==, +, -, !, etc.)➔ Better generics syntax (no wildcards)➔ Automatic delegation pattern syntax➔ String interpolation (idioms)➔ KAPT (Kotlin annotation processor)➔ What about multi-threading, etc? It’s all in Java!

Comparison w/ other emergent (flame war)

➔ Node.js◆ No support to Java ecosystem◆ Java is way faster than Node.js

➔ Go◆ Statically linked (no VM)◆ No support to Java ecosystem◆ JVM is faster than Go

What’s the catch?

➔ Data class can’t inherit or be inherited➔ Classes are final by default (you need to open)➔ Compilation is incremental only in IDEA and Gradle➔ Lack some FP functions (can use funKTionale lib)➔ Null safety checks could be trick in the beginning➔ Some reserved words conflicts (Mockito*)

Roadmap

➔ async/await/yield➔ Data class hierarchy support ➔ Type aliases➔ Bound method references➔ Take advantage of Java 7/8 bytecode enhancements➔ Back to work on JavaScript support

DEMO