Programming language by JetBrains • Targeting Java Platform: server-side, Android, anywhere Java runs • Statically typed Language • Object Oriented and Functional Language • First-class functions • Immutability • No side-effects • Pragmatic, Concise, Safe And Focus on Interoperability
2011 after 1 year development • Seeking language for future development • Scala is great, but compile time and interoperability is obvious deficiency • Open sourced on Feb 2012 • First v1.0 released on 15 Feb 2016 (takes 6 years for first stable release) • Named after Kotlin Island, near St. Petersburg, Russia Where most JetBrains’ Kotlin development team work • “Some part” of language design is based on “Effective Java” book by Joshua Bloch
streams Use RxJava(2) • No lambda, method references Recent Partial Java 8 Desugar Support • No try-with-resources minSdkVersion = 19 or Use Retrolambda
to add methods to platform types in Java • Achieved by Util class • Nullability problems (*NullPointerException) in Java • General verbosity / boilerplate code in Java • Too much code to achieve minimal jobs
age: Int? = null ) fun main() { val persons = listOf( Person("Alice"), Person("Bob", age = 33) ) val oldest = persons.maxBy { it.age ?: 0 } println("The oldest is: $oldest") } // The oldest is: Person(name=Bob, age=33) Data class Nullable type (Int?); default value for argument Top-level function Named argument Lambda expression; Elvis operator String template Autogenerated toString
immutable property, once initialised can’t be changed • var (*variable) for mutable property, can be reassigned with new “data” • fun keyword to declare method/function • So literally we are having fun when code in Kotlin
have return value var say: String = "Kotlin is pragmatic language" say = "also safe and concise" val hello: String = "Domo arigatou Mr. Roboto" hello = "Konnichiwa" // compile time error return Unit } fun sum(x: Int, y: Int): Int { return x + y }
return value var say = "Kotlin is pragmatic language" say = "also safe and concise" val hello = "Domo arigatou Mr. Roboto" hello = "Konnichiwa" // compile time error } fun sum(x: Int, y: Int): Int { return x + y }
return value var say = "Kotlin is pragmatic language" say = "also safe and concise" val hello = "Domo arigatou Mr. Roboto" hello = "Konnichiwa" // compile time error } fun sum(x: Int, y: Int) = x + y // one-liner
email: String? = null ) fun main() { val eko = Contact( name = "Eko Suhariyadi", email = "[email protected]" ) eko.name = null // compile error, because name is non null String eko.email = null // ok, since email is nullable String }
other value when email is null; using elvis operator val email = eko.email ?: "email unavailable" // cascading null safety val emailStrSize = eko.email?.length // safe scoping eko.email?.let { // this is only executed when email is not null // send email }
verbose • Overloading constructor • Getter & setter • toString • hashCode & equals • IDE could help for code generation • Or by using libraries such as AutoValue or Immutable
class TodoKotlin { public final java.lang.String getDescription(); public final void setDescription(java.lang.String); public final long getCreatedAt(); public final void setCreatedAt(long); public final boolean getDone(); public final void setDone(boolean); public TodoKotlin(java.lang.String, long, boolean); public TodoKotlin(java.lang.String, long, boolean, int, kotlin.jvm.internal.DefaultConstructorMarker); public TodoKotlin(java.lang.String, long); public final java.lang.String component1(); public final long component2(); public final boolean component3(); public final TodoKotlin copy(java.lang.String, long, boolean); public static TodoKotlin copy$default(TodoKotlin, java.lang.String, long, boolean, int, java.lang.Object); public java.lang.String toString(); public int hashCode(); public boolean equals(java.lang.Object); }
(description, createAt, finished) = todo // destructure val (description1, _, finished1) = todo // destructure specific field(s) // create new object by cloning from other val cloneTodo = todo.copy(description = "Clone Sample Todo", done = true)
= calculator(numbers) fun main() { val numbers = (1..10).toList() val sum = listCalculator(numbers) { var accumulator = 0 it.forEach { accumulator += it } accumulator } val avg = listCalculator(numbers) { var accumulator = 0 it.forEach { accumulator += 1 } accumulator / numbers.size } println("sum=$sum, avg=$avg") } When function is last parameter We can do this
added to a Type/Class without modifying the original • Function Expressions -> Undeclared function bodies used as an expression (ie. data/ value) • Higher-order Functions -> Function which takes functions as parameter and or returns a function
beginTransaction() try { func() markTransactionSuccessful() } finally { endTransaction() } } // with inline keyword, code block will be inlined with caller code // so, there in no overhead for creating lambda