Share
Embed
Is Kotlin Ready for Android
Peter-John Welcome
July 20, 2017
Transcript
17 slides
-
Kotlin Was born in 2011 by JetBrains Team • Modern
• Expressive • Magical • Powerful • Concise • Extensible https://en.wikipedia.org/wiki/Kotlin_(programming_language) -
Kotlin • Small runtime • Statically typed language • Modern
language • Runs on JVM and converts to Java byte code • Reduces errors and boiler plate code • Composition over inheritance -
Why Kotlin over Java • We still on Java 6.5
in Android • Java Code is error Prone with all its anonymous inner classes • Java Language Restriction ( lambdas , Default Interfaces … etc ) • Java code is very verbose. -
What Java doesn’t have • High-order functions • Properties •
Extension functions • Static null checks • Auto casting/Smart casting • Default parameters • String interpolation • Pattern matching • Inline functions • Infix methods -
"I call it my billion-dollar mistake... [which] has led to
innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years." — Sir Charles Antony Richard Hoare Nullability https://blog.valbonne-consulting.com/2014/11/26/tony-hoare-invention-of-the-null-reference-a-billion-dollar-mistake/ -
var a: String = "foo" a = null //Compilation error
var b: String? = "bar" b = null //ok Nullability -
val user = User() // there's no 'new' keyword in
Kotlin user.name = "PJ" Properties -
inline fun SharedPreferences.edit(action: SharedPreferences.Editor() -> Unit) { val editor =
SharedPreferences.Editor() editor.action() editor.apply() } getSharedPreferences(MODE_PRIVATE).edit { putString("username", username) } Extension Functions for Android -
Anko • Anko Commons: a lightweight library full of helpers
for intents, dialogs, logging and so on; • Anko Layouts: a fast and type-safe way to write dynamic Android layouts; • Anko SQLite: a query DSL and parser collection for Android SQLite; • Anko Coroutines: utilities based on the kotlinx.coroutines library. -
Kotlin advantages • Less crashes • Easier to develop in
• Encourages functional paradigm • 100 % Interoperable with Java ( Can use within existing code base.)