SlideShare a Scribd company logo
Exploring Kotlin on
Android
by Deepanshu & Malcolm
What is Kotlin?
• Statically typed programming language
 
• JVM-based language developed by JetBrains
• From industry not academia
• 100% inter-operable with the Java language
Open Source - https://github.com/JetBrains/kotlin
Why Kotlin
Concise - Drastically reduce the amount of boilerplate code you need to
write.
Safe - Avoid entire classes of errors such as null pointer exceptions.
Versatile - Build server-side applications, Android apps or front-end code
running in the browser.
Kotlin in Expedia Android
Kotlin code is at 23% and rising.
Refactoring started mid 2015.
Hotels, Packages, Flights, Services and MockWebServer & tests
classes are 90% in Kotlin
Cars/LX/Legacy tablet code still in java.
Other teams in Expedia are experimenting with Kotlin in the web.
Features
• Null type and safety
• Lambdas
• Optional params
• Data classes
• Extension functions
• Delegates
• Smart Casting
Not so good about Kotlin!
• Tools support
• Compile time
Kotlin
Roadmap for Android
• Incremental compilation
• support for Jack and Jill toolchain
• Instant run works for cold swaps right now not
for hot swaps
• Lint checks in Kotlin 1.0.2
Kotlin syntax 101
Defining Functions
fun sum(a: Int, b: Int): Int {

return a + b
}
Defining Functions
fun sum(a: Int, b: Int): Int {

return a + b
}
fun sum(a: Int, b: Int) = a + b

fun printSum(a: Int, b: Int): Unit {

print(a + b)

}
Defining Functions
fun printSum(a: Int, b: Int): Unit {

print(a + b)

}
fun printSum(a: Int, b: Int) {

print(a + b)

}
Defining Functions
fun doSomething(): Int {

val a: Int = 1

val b = 1 

val c: Int 

c = a + b



return c

}
Defining local variables
fun doSomething(): Int {

val a: Int = 1

var b = 1
val c: Int
b += 5

c = a + b



return c

}
Defining local variables
Using conditional
expressions
fun doSomething(a: Int, b: Int): Int {

if (a > b)

return a

else

return b

}
Using conditional
expressions
fun doSomething(a: Int, b: Int): Int {

if (a > b)

return a

else

return b

}
fun doSomething(a: Int, b: Int) = if (a > b) a else b

Using a for loop
fun doSomething(args: Array<String>) {

for (arg in args)

print(arg)

}
Using a for loop
fun doSomething(args: Array<String>) {

for (arg in args)

print(arg)

}
fun doSomething(args: Array<String>) {

for (i in args.indices)

print(args[i])

}
Using when expression
fun doSomething(obj: Any) {

when (obj) {

1 -> print("One")

"Hello" -> print("Kotlin")

is Long -> print("Long")

!is String -> print(“!string")

else -> print("Unknown")

}

}
Using ranges
fun doSomething(x: Int, y: Int) {

if (x in 1..y - 1)

print("In")



for (x in 1..5)

print(“N")
if (x !in 1..y - 1)

print(“In")
}
Using ranges
fun doSomething(x: Int, y: Int) {

if (x in 1..y - 1)

print("In")



for (x in 1..5)

print("N")



if (x !in 1..y - 1)

print(“In")


}
Using ranges
fun doSomething(x: Int, y: Int) {

if (x in 1..y - 1)

print("In")



for (x in 1..5)

print("N")



if (x !in 1..y - 1)

print(“In")


}
Class in Java
public class SomeClass {



private String variable;

private final String defaultVar;



SomeClass(String variable) {

this.variable = variable;

this.defaultVar = "Java";

}



SomeClass(String variable, String defaultVar) {

this.variable = variable;

this.defaultVar = defaultVar;

}

}
// Use
new SomeClass("Kotlin", "Java");

new SomeClass("Kotlin");
Class in Kotlin
class SomeClass(var variable: String, val defaultValue: String = "Java") {

}

// Use

SomeClass("Kotlin", "Java")

SomeClass("Kotlin")
Lambdas
fun doSomething() {

val items = ArrayList<String>()

items.sortBy { item ->

item.length

}

}

Lambdas
fun doSomething() {

val items = ArrayList<String>()

items.sortBy { it.length }

}

Lambdas
//In Kotlin
fun doSomething() {

val items = ArrayList<String>()

items.sortBy { it.length }

}
//In Java
ArrayList<String> items = new ArrayList();

Collections.sort(items, new Comparator<String>(){

@Override

public int compare(String s1, String s2) {

return s1.length() - s2.length();

}

});
button.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) {

Toast.makeText(this.MainActivity, "Button",

Toast.LENGTH_LONG).show();

}

});
In Java
button.setOnClickListener(object: View.OnClickListener {

override fun onClick(view: View): Unit {

Toast.makeText(context, "Button",

Toast.LENGTH_LONG).show()

}

})
In Kotlin
button.setOnClickListener { view ->

Toast.makeText(this, "Button",

Toast.LENGTH_LONG).show()

}
In Kotlin
button.setOnClickListener {

Toast.makeText(this, "Button",

Toast.LENGTH_LONG).show()

}
In Kotlin
//In Kotlin
button.setOnClickListener {

Toast.makeText(this, "Button",

Toast.LENGTH_LONG).show()

}
//In Java
button.setOnClickListener(new View.OnClickListener(){

public void onClick(View v) {

Toast.makeText(this.MainActivity, "Button",

Toast.LENGTH_LONG).show();

}

});
class Vehicle(var value: String) {

}



fun doSomething() {

val type = Vehicle("Car")

println("${type.value}")

}
class Vehicle(var value: String) {

}



fun doSomething() {

val type = Vehicle("Car")

println("${type.value}")

}
class Vehicle(var value: String) {

}



fun doSomething() {

val type = Vehicle("Car")

println("${type.value}")

}
class Vehicle(var value: String) {

fun print() = println("$value")

}



fun doSomething() {

val type = Vehicle("Car")

type.print()

}
class Vehicle(var value: String) {

fun print() = println("$value")

}



fun doSomething() {

val type = Vehicle("Car")

type.print()

}
class Vehicle(var value: String) {

fun print() = println("$value")

}



fun doSomething() {

val types = listOf(

Vehicle("Car"),

Vehicle("Truck"),

Vehicle("Bike"))



}
class Vehicle(var value: String) {

fun print() = println("$value")

}



fun doSomething() {

val types = listOf(

Vehicle("Car"),

Vehicle("Truck"),

Vehicle("Bike"))



for (type in types) {

type.print()

}

}

class Vehicle(var value: String) {

fun print() = println("$value")

}



fun doSomething() {

val types = listOf(

Vehicle("Car"),

Vehicle("Truck"),

Vehicle("Bike"))



types.forEach { type ->

type.print()

}

}
class Vehicle(var value: String) {

fun print() = println("$value")

}



fun doSomething() {

val types = listOf(

Vehicle("Car"),

Vehicle("Truck"),

Vehicle("Bike"))



types.forEach { it.print() }

}
class Vehicle(var value: String) {

fun print() = println("$value")

}



fun doSomething() {

val types = listOf(

Vehicle("Car"),

Vehicle("Truck"),

Vehicle("Bike"))



types.forEach { it.print() }

}
class Vehicle(var value: String) {

fun print() = println("$value")

}



fun doSomething() {

listOf(

Vehicle("Car"),

Vehicle("Truck"),

Vehicle("Bike")

).forEach { it.print() }

}
open class Vehicle(var value: String) {

fun print() = println("$value")

}



class Car(var type: String) : Vehicle(type){

}



fun doSomething() {

listOf(

Car("Car"),

Vehicle("Truck"),

Vehicle("Bike")

).forEach{it.print()}

}
open class Vehicle(var value: String) {

fun print() = println("$value")

}



class Car(var type: String) : Vehicle(type){

}



fun doSomething() {

listOf(

Car("Car"),

Vehicle("Truck"),

Vehicle("Bike")

).forEach{it.print()}

}
open class Vehicle(var value: String) {

open fun print() = println("$value")

}



class Car(var type: String) : Vehicle(type){

override fun print() = println("Yo, Its a $value")

}



fun doSomething() {

listOf(

Car("Car"),

Vehicle("Truck"),

Vehicle("Bike")

).forEach{it.print()}

}
open class Vehicle(var value: String) {

open fun print() = println("$value")

}



class Car(var type: String) : Vehicle(type){

override fun print() = println("Yo, Its a $value")

}



fun doSomething() {

listOf(

Car("Car"),

Vehicle("Truck"),

Vehicle("Bike")

).forEach{it.print()}

}
Nullability
I call it my billion-dollar mistake. It was the invention of the null reference …. 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
Null and type safety
fun doSomething() {

var a: String = "abc"

a = null // compilation error

}
fun doSomething() {

var a: String = "abc"

a = null // compilation error



var b: String? = "abc"

b = null // ok

}
Null and type safety
fun doSomething() {

var a: String = "abc"



var b: String? = "abc"

b = null // ok



val l = a.length



}
Null and type safety


fun doSomething() {

var a: String = "abc"



var b: String? = "abc"

b = null // ok



val l1 = a.length



val l2 = b.length // error:variable 'b' can be null



}
Null and type safety
Checking for null in conditions
fun doSomething() {

var a: String = "abc"



var b: String? = "abc"

b = null // ok



val l1 = a.length



val l = if (b != null) b.length else -1



}
Safe Calls
fun doSomething() {

var a: String = "abc"



var b: String? = "abc"

b = null // ok



val l1 = a.length



val l2 = b?.length 



}
Elvis Operator
fun doSomething() {

var a: String = "abc"



var b: String? = "abc"

b = null // ok



val l1 = a.length



val l = if (b != null) b.length else -1



}
fun doSomething() {

var a: String = "abc"



var b: String? = "abc"

b = null // ok



val l1 = a.length



val l2 = b?.length ?: -1


}
if (originLocation != null) {

if (originLocation.hierarchyInfo != null) {

if (originLocation.hierarchyInfo.airport != null) {

if (originLocation.hierarchyInfo.airport.airportCode != null) {

departureAirportCode =
originLocation.hierarchyInfo.airport.airportCode;

}

}



}

}

else {

departureAirportCode = "";

}
val departureAirportCode = originLocation?.hierarchyInfo?.airport?.airportCode ?: ""

if (originLocation != null) {

if (originLocation.hierarchyInfo != null) {

if (originLocation.hierarchyInfo.airport != null) {

if (originLocation.hierarchyInfo.airport.airportCode != null) {

departureAirportCode =
originLocation.hierarchyInfo.airport.airportCode;

}

}



}

}

else {

departureAirportCode = "";

}
The !! Operator
fun doSomething() {

var a: String = "abc"



var b: String? = "abc"

b = null // ok



val l1 = a.length



val l2 = b!!.length


}
Optional-params
//In Java
void foo(int p1, boolean p2) {


}



void foo(int p1) {

foo(1, false);

}



void foo() {

foo(1, false);

}
Optional-params
//In Kotlin
fun foo(p1: Int = 1, p2: Boolean = false) {



}
foo()

foo(3)
Data class
data class Traveler(val name: String?, val age: Int)
Automatically implements:
equals()/hashCode()

toString() of the form "User(name=John, age=42)",

copy() function
val testTravlerJohn = Traveler(“John”, 42)
val testTravlerJane = Traveler(“Jane”, 41)
assertEquals(testTravlerJohn, testTravlerJane)
Data class in Testing
public class Traveler(String name, Int age)
@Override

public boolean equals(Object o) {

if (this == o) {

return true;

}

if (!(o instanceof Traveler)) {

return false;

}



Traveler traveler = (Traveler) o;



if (name != name) {

return false;

}

if (age != traveler.age) {

return false;

}
return true;
}
Extension Functions
//In Java
public class StrUtils {

public static String encodeString(String str) {

return str.replaceAll(" ", "_")

}

}
String text = "Hello World";

System.out.println(StrUtils.encodeString(text));


Extension Functions
//In Kotlin
fun String.encodeSpaces(): String {

return this.replaceAll(" ", "_")

}
val text = "hello world"



println(text.encodeSpaces())
val list = listOf("E", "H", "Hello", "Hi", "I")

val map = mapOf(Pair("A", 1), Pair("B", 2), Pair("C", 3))



list.forEach { s ->

s.length

}
"E, H, Hello, Hi, I"



Kotlin Collections Extensions
val list = listOf("E", "H", "Hello", "Hi", "I")

val map = mapOf(Pair("A", 1), Pair("B", 2), Pair("C", 3))



var count = 0

list.forEachIndexed { i, s ->

count += i

s.length

}


"E, H, Hello, Hi, I"



val list = listOf("E", "H", "Hello", "Hi", "I")

val map = mapOf(Pair("A", 1), Pair("B", 2), Pair("C", 3))



list.filter { s ->

s.contains("H")

}

"H, Hello, Hi"



val list = listOf("E", "H", "Hello", "Hi", "I")

val map = mapOf(Pair("A", 1), Pair("B", 2), Pair("C", 3))



list.first { s ->

s.startsWith("He")

}

"Hello"



val list = listOf("E", "H", "Hello", "Hi", "I")

val map = mapOf(Pair("A", 1), Pair("B", 2), Pair("C", 3))



map.forEach { entry ->

entry.value
}

"A, B, C"
//In Java
final int[] sponsoredIndexes = { 0, 50, 51 };



ArrayList<Property> sponsored = new ArrayList<Property>();

ListIterator<Property> it = properties.listIterator();

while (it.hasNext()) {

Property prop = it.next();

if (prop.isSponsored()) {

it.remove();

sponsored.add(prop);

}

}



for (int i = 0; i < sponsored.size() && i < sponsoredIndexes.length; i++) {

if (sponsoredIndexes[i] <= properties.size()) {

properties.add(sponsoredIndexes[i], sponsored.get(i));

}

}
//In Kotlin
fun putSponsoredItemsInCorrectPlaces(hotelList: List<Hotel>): List<Hotel> {

val (sponsored, nonSponsored) = hotelList.partition { it.isSponsoredListing }

val firstChunk = sponsored.take(1)

val secondChunk = nonSponsored.take(49)

val thirdChunk = sponsored.drop(1)

val rest = nonSponsored.drop(49)

return firstChunk + secondChunk + thirdChunk + rest

}
//In Java
Observable.just("Hello World")

.map(new Func1<String, Object>() {



@Override

public Object call(String s) {

return s + "!";

}

})

.subscribe(new Subscriber<String>() {

@Override

public void onCompleted() {

//Completion

}



@Override

public void onError(final Throwable e) {

//TODO : Handle error here

}



@Override

public void onNext(final String s) {

Log.e("Output",s);

}

});
Reative Java
Kotlin & Reactive
Observable.just("Hello World")

.map { it.plus("!") }

.subscribe { println("Output:" + it); }
Flow sensitive typing
//In Java
@Override

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

if (holder.getItemViewType() != LOADING_VIEW) {

((ViewHolder) holder).updateHotel();

}

else if (holder.getItemViewType() != HOTEL_VIEW) {

((LoadingViewHolder) holder).loadView();

}

}

Flow sensitive typing
//In Kotlin
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

when (holder) {

is HotelCellViewHolder-> holder.updateHotel()

is LoadingViewHolder -> holder.loadView()

}

}
Delegates
var p: String by Delegate()
Properties using the delegate expression will have its
get() and set() methods delegated to the expression.
* lazy
* observable
* notNull
val view: Presenter by lazy {

var view = stub.inflate() as Presenter

view

}
Delegates.lazy
Delegates.notNull
class Foo {

var bar : Bar by Delegates.notNull() // type is now Bar



init {

val s = bar // Using before setting throws an IllegalStateException!

bar = Bar()

}

}
Delegates.observable
val text: String by Delegates.observable("") {

prop, old, new ->

println("$old -> $new")

}
Custom delegate(KotterKnife)
fun <T : View> ViewGroup.bindView(id: Int): ReadOnlyProperty<Any, T> =
ViewBinding(this, id)
private class ViewBinding<T : View>(val source: Any, val id: Int) :
ReadOnlyProperty<Any, T> {

private val lazy = Lazy<T>()



override fun getValue(thisRef: Any, property: KProperty<*>): T = lazy.get {

findView<T>(source, id)

?: throw IllegalStateException("View ID $id for '${property.name}'
not found.")

}

}
val mapView: MapView by bindView(R.id.map_view)
Questions?

More Related Content

What's hot

eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017
eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017
eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017
eMan s.r.o.
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++
kinan keshkeh
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
Yandex
 
The present and the future of functional programming in c++
The present and the future of functional programming in c++The present and the future of functional programming in c++
The present and the future of functional programming in c++
Alexander Granin
 
Monadic parsers in C++
Monadic parsers in C++Monadic parsers in C++
Monadic parsers in C++
Alexander Granin
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java API
Mario Fusco
 
How do you create a programming language for the JVM?
How do you create a programming language for the JVM?How do you create a programming language for the JVM?
How do you create a programming language for the JVM?
Federico Tomassetti
 
Fun with Kotlin
Fun with KotlinFun with Kotlin
Fun with Kotlin
Egor Andreevich
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Feature
xcoda
 
Gremlin's Graph Traversal Machinery
Gremlin's Graph Traversal MachineryGremlin's Graph Traversal Machinery
Gremlin's Graph Traversal Machinery
Marko Rodriguez
 
Towards Domain Refinement for UML/OCL Bounded Verification
Towards Domain Refinement for UML/OCL Bounded VerificationTowards Domain Refinement for UML/OCL Bounded Verification
Towards Domain Refinement for UML/OCL Bounded Verification
rclariso
 
Bridges
BridgesBridges
Bridges
Pere Villega
 
Kotlin as a Better Java
Kotlin as a Better JavaKotlin as a Better Java
Kotlin as a Better Java
Garth Gilmour
 
ZIO Queue
ZIO QueueZIO Queue
ZIO Queue
John De Goes
 
Scalaz Stream: Rebirth
Scalaz Stream: RebirthScalaz Stream: Rebirth
Scalaz Stream: Rebirth
John De Goes
 
Functional programming for production quality code
Functional programming for production quality codeFunctional programming for production quality code
Functional programming for production quality code
Jack Fox
 
Swift Introduction
Swift IntroductionSwift Introduction
Swift Introduction
Giuseppe Arici
 
Monadic Java
Monadic JavaMonadic Java
Monadic Java
Codemotion
 
No more loops with lambdaj
No more loops with lambdajNo more loops with lambdaj
No more loops with lambdaj
Mario Fusco
 
Optimizing last mile delivery
Optimizing last mile deliveryOptimizing last mile delivery
Optimizing last mile delivery
YashwanthKumarYelama
 

What's hot (20)

eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017
eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017
eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
 
The present and the future of functional programming in c++
The present and the future of functional programming in c++The present and the future of functional programming in c++
The present and the future of functional programming in c++
 
Monadic parsers in C++
Monadic parsers in C++Monadic parsers in C++
Monadic parsers in C++
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java API
 
How do you create a programming language for the JVM?
How do you create a programming language for the JVM?How do you create a programming language for the JVM?
How do you create a programming language for the JVM?
 
Fun with Kotlin
Fun with KotlinFun with Kotlin
Fun with Kotlin
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Feature
 
Gremlin's Graph Traversal Machinery
Gremlin's Graph Traversal MachineryGremlin's Graph Traversal Machinery
Gremlin's Graph Traversal Machinery
 
Towards Domain Refinement for UML/OCL Bounded Verification
Towards Domain Refinement for UML/OCL Bounded VerificationTowards Domain Refinement for UML/OCL Bounded Verification
Towards Domain Refinement for UML/OCL Bounded Verification
 
Bridges
BridgesBridges
Bridges
 
Kotlin as a Better Java
Kotlin as a Better JavaKotlin as a Better Java
Kotlin as a Better Java
 
ZIO Queue
ZIO QueueZIO Queue
ZIO Queue
 
Scalaz Stream: Rebirth
Scalaz Stream: RebirthScalaz Stream: Rebirth
Scalaz Stream: Rebirth
 
Functional programming for production quality code
Functional programming for production quality codeFunctional programming for production quality code
Functional programming for production quality code
 
Swift Introduction
Swift IntroductionSwift Introduction
Swift Introduction
 
Monadic Java
Monadic JavaMonadic Java
Monadic Java
 
No more loops with lambdaj
No more loops with lambdajNo more loops with lambdaj
No more loops with lambdaj
 
Optimizing last mile delivery
Optimizing last mile deliveryOptimizing last mile delivery
Optimizing last mile delivery
 

Similar to Exploring Koltin on Android

Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kirill Rozov
 
Kotlin
KotlinKotlin
NUS iOS Swift Talk
NUS iOS Swift TalkNUS iOS Swift Talk
NUS iOS Swift Talk
Gabriel Lim
 
Kotlin Starter Pack
Kotlin Starter PackKotlin Starter Pack
Kotlin Starter Pack
Jimmy Morales
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvm
Arnaud Giuliani
 
01 Introduction to Kotlin - Programming in Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx01 Introduction to Kotlin - Programming in Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx
IvanZawPhyo
 
Introduction to Kotlin.pptx
Introduction to Kotlin.pptxIntroduction to Kotlin.pptx
Introduction to Kotlin.pptx
AzharFauzan9
 
XKE Typeclass
XKE TypeclassXKE Typeclass
XKE Typeclass
Mathieu DULAC
 
Let's fly to the Kotlin Island. Just an introduction to Kotlin
Let's fly to the Kotlin Island. Just an introduction to KotlinLet's fly to the Kotlin Island. Just an introduction to Kotlin
Let's fly to the Kotlin Island. Just an introduction to Kotlin
Aliaksei Zhynhiarouski
 
Why Kotlin is your next language?
Why Kotlin is your next language? Why Kotlin is your next language?
Why Kotlin is your next language?
Aliaksei Zhynhiarouski
 
Kotlin Overview (PT-BR)
Kotlin Overview (PT-BR)Kotlin Overview (PT-BR)
Kotlin Overview (PT-BR)
ThomasHorta
 
How to not write a boring test in Golang
How to not write a boring test in GolangHow to not write a boring test in Golang
How to not write a boring test in Golang
Dan Tran
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
Kaz Yoshikawa
 
Swift School #1
Swift School #1Swift School #1
Swift School #1
Sergey Pronin
 
Kotlin coroutines and spring framework
Kotlin coroutines and spring frameworkKotlin coroutines and spring framework
Kotlin coroutines and spring framework
Sunghyouk Bae
 
KotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptxKotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptx
Ian Robertson
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)
Gesh Markov
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
priort
 
TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason Haffey
Ralph Johnson
 
Android dev by dilip baliga.pptx
Android dev by dilip baliga.pptxAndroid dev by dilip baliga.pptx
Android dev by dilip baliga.pptx
AnanyaPRao
 

Similar to Exploring Koltin on Android (20)

Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 
Kotlin
KotlinKotlin
Kotlin
 
NUS iOS Swift Talk
NUS iOS Swift TalkNUS iOS Swift Talk
NUS iOS Swift Talk
 
Kotlin Starter Pack
Kotlin Starter PackKotlin Starter Pack
Kotlin Starter Pack
 
Kotlin, smarter development for the jvm
Kotlin, smarter development for the jvmKotlin, smarter development for the jvm
Kotlin, smarter development for the jvm
 
01 Introduction to Kotlin - Programming in Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx01 Introduction to Kotlin - Programming in Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx
 
Introduction to Kotlin.pptx
Introduction to Kotlin.pptxIntroduction to Kotlin.pptx
Introduction to Kotlin.pptx
 
XKE Typeclass
XKE TypeclassXKE Typeclass
XKE Typeclass
 
Let's fly to the Kotlin Island. Just an introduction to Kotlin
Let's fly to the Kotlin Island. Just an introduction to KotlinLet's fly to the Kotlin Island. Just an introduction to Kotlin
Let's fly to the Kotlin Island. Just an introduction to Kotlin
 
Why Kotlin is your next language?
Why Kotlin is your next language? Why Kotlin is your next language?
Why Kotlin is your next language?
 
Kotlin Overview (PT-BR)
Kotlin Overview (PT-BR)Kotlin Overview (PT-BR)
Kotlin Overview (PT-BR)
 
How to not write a boring test in Golang
How to not write a boring test in GolangHow to not write a boring test in Golang
How to not write a boring test in Golang
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
 
Swift School #1
Swift School #1Swift School #1
Swift School #1
 
Kotlin coroutines and spring framework
Kotlin coroutines and spring frameworkKotlin coroutines and spring framework
Kotlin coroutines and spring framework
 
KotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptxKotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptx
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
 
TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason Haffey
 
Android dev by dilip baliga.pptx
Android dev by dilip baliga.pptxAndroid dev by dilip baliga.pptx
Android dev by dilip baliga.pptx
 

Exploring Koltin on Android

  • 1. Exploring Kotlin on Android by Deepanshu & Malcolm
  • 2. What is Kotlin? • Statically typed programming language   • JVM-based language developed by JetBrains • From industry not academia • 100% inter-operable with the Java language Open Source - https://github.com/JetBrains/kotlin
  • 3. Why Kotlin Concise - Drastically reduce the amount of boilerplate code you need to write. Safe - Avoid entire classes of errors such as null pointer exceptions. Versatile - Build server-side applications, Android apps or front-end code running in the browser.
  • 4. Kotlin in Expedia Android Kotlin code is at 23% and rising. Refactoring started mid 2015. Hotels, Packages, Flights, Services and MockWebServer & tests classes are 90% in Kotlin Cars/LX/Legacy tablet code still in java. Other teams in Expedia are experimenting with Kotlin in the web.
  • 5. Features • Null type and safety • Lambdas • Optional params • Data classes • Extension functions • Delegates • Smart Casting
  • 6. Not so good about Kotlin! • Tools support • Compile time
  • 7. Kotlin Roadmap for Android • Incremental compilation • support for Jack and Jill toolchain • Instant run works for cold swaps right now not for hot swaps • Lint checks in Kotlin 1.0.2
  • 9. Defining Functions fun sum(a: Int, b: Int): Int {
 return a + b }
  • 10. Defining Functions fun sum(a: Int, b: Int): Int {
 return a + b } fun sum(a: Int, b: Int) = a + b

  • 11. fun printSum(a: Int, b: Int): Unit {
 print(a + b)
 } Defining Functions
  • 12. fun printSum(a: Int, b: Int): Unit {
 print(a + b)
 } fun printSum(a: Int, b: Int) {
 print(a + b)
 } Defining Functions
  • 13. fun doSomething(): Int {
 val a: Int = 1
 val b = 1 
 val c: Int 
 c = a + b
 
 return c
 } Defining local variables
  • 14. fun doSomething(): Int {
 val a: Int = 1
 var b = 1 val c: Int b += 5
 c = a + b
 
 return c
 } Defining local variables
  • 15. Using conditional expressions fun doSomething(a: Int, b: Int): Int {
 if (a > b)
 return a
 else
 return b
 }
  • 16. Using conditional expressions fun doSomething(a: Int, b: Int): Int {
 if (a > b)
 return a
 else
 return b
 } fun doSomething(a: Int, b: Int) = if (a > b) a else b

  • 17. Using a for loop fun doSomething(args: Array<String>) {
 for (arg in args)
 print(arg)
 }
  • 18. Using a for loop fun doSomething(args: Array<String>) {
 for (arg in args)
 print(arg)
 } fun doSomething(args: Array<String>) {
 for (i in args.indices)
 print(args[i])
 }
  • 19. Using when expression fun doSomething(obj: Any) {
 when (obj) {
 1 -> print("One")
 "Hello" -> print("Kotlin")
 is Long -> print("Long")
 !is String -> print(“!string")
 else -> print("Unknown")
 }
 }
  • 20. Using ranges fun doSomething(x: Int, y: Int) {
 if (x in 1..y - 1)
 print("In")
 
 for (x in 1..5)
 print(“N") if (x !in 1..y - 1)
 print(“In") }
  • 21. Using ranges fun doSomething(x: Int, y: Int) {
 if (x in 1..y - 1)
 print("In")
 
 for (x in 1..5)
 print("N")
 
 if (x !in 1..y - 1)
 print(“In") 
 }
  • 22. Using ranges fun doSomething(x: Int, y: Int) {
 if (x in 1..y - 1)
 print("In")
 
 for (x in 1..5)
 print("N")
 
 if (x !in 1..y - 1)
 print(“In") 
 }
  • 23. Class in Java public class SomeClass {
 
 private String variable;
 private final String defaultVar;
 
 SomeClass(String variable) {
 this.variable = variable;
 this.defaultVar = "Java";
 }
 
 SomeClass(String variable, String defaultVar) {
 this.variable = variable;
 this.defaultVar = defaultVar;
 }
 } // Use new SomeClass("Kotlin", "Java");
 new SomeClass("Kotlin");
  • 24. Class in Kotlin class SomeClass(var variable: String, val defaultValue: String = "Java") {
 }
 // Use
 SomeClass("Kotlin", "Java")
 SomeClass("Kotlin")
  • 25. Lambdas fun doSomething() {
 val items = ArrayList<String>()
 items.sortBy { item ->
 item.length
 }
 }

  • 26. Lambdas fun doSomething() {
 val items = ArrayList<String>()
 items.sortBy { it.length }
 }

  • 27. Lambdas //In Kotlin fun doSomething() {
 val items = ArrayList<String>()
 items.sortBy { it.length }
 } //In Java ArrayList<String> items = new ArrayList();
 Collections.sort(items, new Comparator<String>(){
 @Override
 public int compare(String s1, String s2) {
 return s1.length() - s2.length();
 }
 });
  • 28. button.setOnClickListener(new View.OnClickListener(){
 public void onClick(View v) {
 Toast.makeText(this.MainActivity, "Button",
 Toast.LENGTH_LONG).show();
 }
 }); In Java
  • 29. button.setOnClickListener(object: View.OnClickListener {
 override fun onClick(view: View): Unit {
 Toast.makeText(context, "Button",
 Toast.LENGTH_LONG).show()
 }
 }) In Kotlin
  • 30. button.setOnClickListener { view ->
 Toast.makeText(this, "Button",
 Toast.LENGTH_LONG).show()
 } In Kotlin
  • 32. //In Kotlin button.setOnClickListener {
 Toast.makeText(this, "Button",
 Toast.LENGTH_LONG).show()
 } //In Java button.setOnClickListener(new View.OnClickListener(){
 public void onClick(View v) {
 Toast.makeText(this.MainActivity, "Button",
 Toast.LENGTH_LONG).show();
 }
 });
  • 33. class Vehicle(var value: String) {
 }
 
 fun doSomething() {
 val type = Vehicle("Car")
 println("${type.value}")
 }
  • 34. class Vehicle(var value: String) {
 }
 
 fun doSomething() {
 val type = Vehicle("Car")
 println("${type.value}")
 }
  • 35. class Vehicle(var value: String) {
 }
 
 fun doSomething() {
 val type = Vehicle("Car")
 println("${type.value}")
 }
  • 36. class Vehicle(var value: String) {
 fun print() = println("$value")
 }
 
 fun doSomething() {
 val type = Vehicle("Car")
 type.print()
 }
  • 37. class Vehicle(var value: String) {
 fun print() = println("$value")
 }
 
 fun doSomething() {
 val type = Vehicle("Car")
 type.print()
 }
  • 38. class Vehicle(var value: String) {
 fun print() = println("$value")
 }
 
 fun doSomething() {
 val types = listOf(
 Vehicle("Car"),
 Vehicle("Truck"),
 Vehicle("Bike"))
 
 }
  • 39. class Vehicle(var value: String) {
 fun print() = println("$value")
 }
 
 fun doSomething() {
 val types = listOf(
 Vehicle("Car"),
 Vehicle("Truck"),
 Vehicle("Bike"))
 
 for (type in types) {
 type.print()
 }
 }

  • 40. class Vehicle(var value: String) {
 fun print() = println("$value")
 }
 
 fun doSomething() {
 val types = listOf(
 Vehicle("Car"),
 Vehicle("Truck"),
 Vehicle("Bike"))
 
 types.forEach { type ->
 type.print()
 }
 }
  • 41. class Vehicle(var value: String) {
 fun print() = println("$value")
 }
 
 fun doSomething() {
 val types = listOf(
 Vehicle("Car"),
 Vehicle("Truck"),
 Vehicle("Bike"))
 
 types.forEach { it.print() }
 }
  • 42. class Vehicle(var value: String) {
 fun print() = println("$value")
 }
 
 fun doSomething() {
 val types = listOf(
 Vehicle("Car"),
 Vehicle("Truck"),
 Vehicle("Bike"))
 
 types.forEach { it.print() }
 }
  • 43. class Vehicle(var value: String) {
 fun print() = println("$value")
 }
 
 fun doSomething() {
 listOf(
 Vehicle("Car"),
 Vehicle("Truck"),
 Vehicle("Bike")
 ).forEach { it.print() }
 }
  • 44. open class Vehicle(var value: String) {
 fun print() = println("$value")
 }
 
 class Car(var type: String) : Vehicle(type){
 }
 
 fun doSomething() {
 listOf(
 Car("Car"),
 Vehicle("Truck"),
 Vehicle("Bike")
 ).forEach{it.print()}
 }
  • 45. open class Vehicle(var value: String) {
 fun print() = println("$value")
 }
 
 class Car(var type: String) : Vehicle(type){
 }
 
 fun doSomething() {
 listOf(
 Car("Car"),
 Vehicle("Truck"),
 Vehicle("Bike")
 ).forEach{it.print()}
 }
  • 46. open class Vehicle(var value: String) {
 open fun print() = println("$value")
 }
 
 class Car(var type: String) : Vehicle(type){
 override fun print() = println("Yo, Its a $value")
 }
 
 fun doSomething() {
 listOf(
 Car("Car"),
 Vehicle("Truck"),
 Vehicle("Bike")
 ).forEach{it.print()}
 }
  • 47. open class Vehicle(var value: String) {
 open fun print() = println("$value")
 }
 
 class Car(var type: String) : Vehicle(type){
 override fun print() = println("Yo, Its a $value")
 }
 
 fun doSomething() {
 listOf(
 Car("Car"),
 Vehicle("Truck"),
 Vehicle("Bike")
 ).forEach{it.print()}
 }
  • 48. Nullability I call it my billion-dollar mistake. It was the invention of the null reference …. 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
  • 49. Null and type safety fun doSomething() {
 var a: String = "abc"
 a = null // compilation error
 }
  • 50. fun doSomething() {
 var a: String = "abc"
 a = null // compilation error
 
 var b: String? = "abc"
 b = null // ok
 } Null and type safety
  • 51. fun doSomething() {
 var a: String = "abc"
 
 var b: String? = "abc"
 b = null // ok
 
 val l = a.length
 
 } Null and type safety
  • 52. 
 fun doSomething() {
 var a: String = "abc"
 
 var b: String? = "abc"
 b = null // ok
 
 val l1 = a.length
 
 val l2 = b.length // error:variable 'b' can be null
 
 } Null and type safety
  • 53. Checking for null in conditions fun doSomething() {
 var a: String = "abc"
 
 var b: String? = "abc"
 b = null // ok
 
 val l1 = a.length
 
 val l = if (b != null) b.length else -1
 
 }
  • 54. Safe Calls fun doSomething() {
 var a: String = "abc"
 
 var b: String? = "abc"
 b = null // ok
 
 val l1 = a.length
 
 val l2 = b?.length 
 
 }
  • 55. Elvis Operator fun doSomething() {
 var a: String = "abc"
 
 var b: String? = "abc"
 b = null // ok
 
 val l1 = a.length
 
 val l = if (b != null) b.length else -1
 
 }
  • 56. fun doSomething() {
 var a: String = "abc"
 
 var b: String? = "abc"
 b = null // ok
 
 val l1 = a.length
 
 val l2 = b?.length ?: -1 
 }
  • 57. if (originLocation != null) {
 if (originLocation.hierarchyInfo != null) {
 if (originLocation.hierarchyInfo.airport != null) {
 if (originLocation.hierarchyInfo.airport.airportCode != null) {
 departureAirportCode = originLocation.hierarchyInfo.airport.airportCode;
 }
 }
 
 }
 }
 else {
 departureAirportCode = "";
 }
  • 58. val departureAirportCode = originLocation?.hierarchyInfo?.airport?.airportCode ?: ""
 if (originLocation != null) {
 if (originLocation.hierarchyInfo != null) {
 if (originLocation.hierarchyInfo.airport != null) {
 if (originLocation.hierarchyInfo.airport.airportCode != null) {
 departureAirportCode = originLocation.hierarchyInfo.airport.airportCode;
 }
 }
 
 }
 }
 else {
 departureAirportCode = "";
 }
  • 59. The !! Operator fun doSomething() {
 var a: String = "abc"
 
 var b: String? = "abc"
 b = null // ok
 
 val l1 = a.length
 
 val l2 = b!!.length 
 }
  • 60. Optional-params //In Java void foo(int p1, boolean p2) { 
 }
 
 void foo(int p1) {
 foo(1, false);
 }
 
 void foo() {
 foo(1, false);
 }
  • 61. Optional-params //In Kotlin fun foo(p1: Int = 1, p2: Boolean = false) {
 
 } foo()
 foo(3)
  • 62. Data class data class Traveler(val name: String?, val age: Int) Automatically implements: equals()/hashCode()
 toString() of the form "User(name=John, age=42)",
 copy() function
  • 63. val testTravlerJohn = Traveler(“John”, 42) val testTravlerJane = Traveler(“Jane”, 41) assertEquals(testTravlerJohn, testTravlerJane) Data class in Testing
  • 64. public class Traveler(String name, Int age) @Override
 public boolean equals(Object o) {
 if (this == o) {
 return true;
 }
 if (!(o instanceof Traveler)) {
 return false;
 }
 
 Traveler traveler = (Traveler) o;
 
 if (name != name) {
 return false;
 }
 if (age != traveler.age) {
 return false;
 } return true; }
  • 65. Extension Functions //In Java public class StrUtils {
 public static String encodeString(String str) {
 return str.replaceAll(" ", "_")
 }
 } String text = "Hello World";
 System.out.println(StrUtils.encodeString(text)); 

  • 66. Extension Functions //In Kotlin fun String.encodeSpaces(): String {
 return this.replaceAll(" ", "_")
 } val text = "hello world"
 
 println(text.encodeSpaces())
  • 67. val list = listOf("E", "H", "Hello", "Hi", "I")
 val map = mapOf(Pair("A", 1), Pair("B", 2), Pair("C", 3))
 
 list.forEach { s ->
 s.length
 } "E, H, Hello, Hi, I"
 
 Kotlin Collections Extensions
  • 68. val list = listOf("E", "H", "Hello", "Hi", "I")
 val map = mapOf(Pair("A", 1), Pair("B", 2), Pair("C", 3))
 
 var count = 0
 list.forEachIndexed { i, s ->
 count += i
 s.length
 } 
 "E, H, Hello, Hi, I"
 

  • 69. val list = listOf("E", "H", "Hello", "Hi", "I")
 val map = mapOf(Pair("A", 1), Pair("B", 2), Pair("C", 3))
 
 list.filter { s ->
 s.contains("H")
 }
 "H, Hello, Hi"
 

  • 70. val list = listOf("E", "H", "Hello", "Hi", "I")
 val map = mapOf(Pair("A", 1), Pair("B", 2), Pair("C", 3))
 
 list.first { s ->
 s.startsWith("He")
 }
 "Hello"
 

  • 71. val list = listOf("E", "H", "Hello", "Hi", "I")
 val map = mapOf(Pair("A", 1), Pair("B", 2), Pair("C", 3))
 
 map.forEach { entry ->
 entry.value }
 "A, B, C"
  • 72. //In Java final int[] sponsoredIndexes = { 0, 50, 51 };
 
 ArrayList<Property> sponsored = new ArrayList<Property>();
 ListIterator<Property> it = properties.listIterator();
 while (it.hasNext()) {
 Property prop = it.next();
 if (prop.isSponsored()) {
 it.remove();
 sponsored.add(prop);
 }
 }
 
 for (int i = 0; i < sponsored.size() && i < sponsoredIndexes.length; i++) {
 if (sponsoredIndexes[i] <= properties.size()) {
 properties.add(sponsoredIndexes[i], sponsored.get(i));
 }
 }
  • 73. //In Kotlin fun putSponsoredItemsInCorrectPlaces(hotelList: List<Hotel>): List<Hotel> {
 val (sponsored, nonSponsored) = hotelList.partition { it.isSponsoredListing }
 val firstChunk = sponsored.take(1)
 val secondChunk = nonSponsored.take(49)
 val thirdChunk = sponsored.drop(1)
 val rest = nonSponsored.drop(49)
 return firstChunk + secondChunk + thirdChunk + rest
 }
  • 74. //In Java Observable.just("Hello World")
 .map(new Func1<String, Object>() {
 
 @Override
 public Object call(String s) {
 return s + "!";
 }
 })
 .subscribe(new Subscriber<String>() {
 @Override
 public void onCompleted() {
 //Completion
 }
 
 @Override
 public void onError(final Throwable e) {
 //TODO : Handle error here
 }
 
 @Override
 public void onNext(final String s) {
 Log.e("Output",s);
 }
 }); Reative Java
  • 75. Kotlin & Reactive Observable.just("Hello World")
 .map { it.plus("!") }
 .subscribe { println("Output:" + it); }
  • 76. Flow sensitive typing //In Java @Override
 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
 if (holder.getItemViewType() != LOADING_VIEW) {
 ((ViewHolder) holder).updateHotel();
 }
 else if (holder.getItemViewType() != HOTEL_VIEW) {
 ((LoadingViewHolder) holder).loadView();
 }
 }

  • 77. Flow sensitive typing //In Kotlin override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
 when (holder) {
 is HotelCellViewHolder-> holder.updateHotel()
 is LoadingViewHolder -> holder.loadView()
 }
 }
  • 78. Delegates var p: String by Delegate() Properties using the delegate expression will have its get() and set() methods delegated to the expression. * lazy * observable * notNull
  • 79. val view: Presenter by lazy {
 var view = stub.inflate() as Presenter
 view
 } Delegates.lazy
  • 80. Delegates.notNull class Foo {
 var bar : Bar by Delegates.notNull() // type is now Bar
 
 init {
 val s = bar // Using before setting throws an IllegalStateException!
 bar = Bar()
 }
 }
  • 81. Delegates.observable val text: String by Delegates.observable("") {
 prop, old, new ->
 println("$old -> $new")
 }
  • 82. Custom delegate(KotterKnife) fun <T : View> ViewGroup.bindView(id: Int): ReadOnlyProperty<Any, T> = ViewBinding(this, id) private class ViewBinding<T : View>(val source: Any, val id: Int) : ReadOnlyProperty<Any, T> {
 private val lazy = Lazy<T>()
 
 override fun getValue(thisRef: Any, property: KProperty<*>): T = lazy.get {
 findView<T>(source, id)
 ?: throw IllegalStateException("View ID $id for '${property.name}' not found.")
 }
 } val mapView: MapView by bindView(R.id.map_view)