Fun with Kotlin

60
Fun With Kotlin by Egor Andreevici

Transcript of Fun with Kotlin

Page 1: Fun with Kotlin

Fun WithKotlin

by Egor Andreevici

Page 2: Fun with Kotlin

INTRO

WHAT’S KOTLIN

Page 3: Fun with Kotlin

INTRO

WHY KOTLIN?

Page 4: Fun with Kotlin

INTRO

PRAGMATISM

Clarity Safety Interoperability Tooling

WHAT’S KOTLIN ABOUT?

Page 5: Fun with Kotlin

INTRO

FOCUS ON USE CASESINTEROPERATE

WHY PRAGMATIC?

Page 6: Fun with Kotlin

INTRO

WHO MAINTAINS KOTLIN?

Page 7: Fun with Kotlin

INTRO

WHO’S USING KOTLIN?

Page 8: Fun with Kotlin

SYNTAX

Page 9: Fun with Kotlin

SYNTAX

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

KOTLIN SYNTAX

Page 10: Fun with Kotlin

SYNTAX

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

KOTLIN SYNTAX

Page 11: Fun with Kotlin

SYNTAX

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

KOTLIN SYNTAX

Page 12: Fun with Kotlin

SYNTAX

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

KOTLIN SYNTAX

Page 13: Fun with Kotlin

SYNTAX

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

KOTLIN SYNTAX

Page 14: Fun with Kotlin

SYNTAX

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

KOTLIN SYNTAX

Page 15: Fun with Kotlin

SYNTAX

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

KOTLIN SYNTAX

Page 16: Fun with Kotlin

SYNTAX

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

KOTLIN SYNTAX

Page 17: Fun with Kotlin

CLASSES

Page 18: Fun with Kotlin

CLASSES

Kotlin Java

class Person public class Person {}

CREATING A CLASS

Page 19: Fun with Kotlin

CLASSES

class Person

class Student : Person()

INHERITANCE

Page 20: Fun with Kotlin

CLASSES

class Person

class Student : Person() // won’t compile!

INHERITANCE

Page 21: Fun with Kotlin

CLASSES

class Person

class Student : Person() // won’t compile!

INHERITANCE

Page 22: Fun with Kotlin

CLASSES

open class Person

class Student : Person() // voila!

INHERITANCE

Page 23: Fun with Kotlin

CLASSES

open class Person(val name: String, var age: Int)

// Javapublic class Person { private final String name; private Integer age;

public Person(String name, Integer age) { // }

public String getName() { // } public Integer getAge() { // } public void setAge(Integer age) { // }}

CONSTRUCTORS

Page 24: Fun with Kotlin

CLASSES

open class Person(val name: String, var age: Int)

Kotlin Java

val name: String final String name

var age: Int Integer age

VAL & VAR

Page 25: Fun with Kotlin

CLASSES

data class Person(val name: String, var age: Int)

‣ equals() & hashCode() $0

‣ toString() $0

‣ componentN() functions $0

‣ copy() function $0

‣ TOTAL FREE!

DATA CLASSES

Page 26: Fun with Kotlin

CLASSES

val name = person.component1()val age = person.component2()

COMPONENTN() FUNCTIONS

Page 27: Fun with Kotlin

CLASSES

val name = person.component1()val age = person.component2()

for ((name, age) in people) { println("$name is $age years old")}

DESTRUCTURING DECLARATIONS

Page 28: Fun with Kotlin

CLASSES

class Person { companion object { val FIELD_NAME = "name"

fun isOlderThan(person: Person, age: Int) = person.age > age }}

println(Person.isOlderThan(person, 30))

STATIC -> OBJECT

Page 29: Fun with Kotlin

CLASSES

public final class StringUtils {

public static String capitalize(String str) { // TODO: return capitalized string return str; }}

StringUtils.capitalize(“a”);

UTILS IN JAVA

Page 30: Fun with Kotlin

CLASSES

fun String.capitalize(): String { // TODO: return capitalized string return this}

"a".capitalize()

EXTENSIONS IN KOTLIN

Page 31: Fun with Kotlin

CLASSES

fun String.capitalize(): String { // TODO: return capitalized string return this}

"a".capitalize()

import me.egorand.kotlin.extensions.capitalize

EXTENSIONS IN KOTLIN

Page 32: Fun with Kotlin

NULL-SAFETY

Page 33: Fun with Kotlin

NULL-SAFETY

THE BILLION DOLLAR MISTAKE

Page 34: Fun with Kotlin

NULL SAFETY

var a: String = "abc"a = null // compilation error

var b: String? = "abc" b = null // OK

val l = a.length

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

NULLABLE VS NON-NULL TYPES

Page 35: Fun with Kotlin

NULL SAFETY

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

CHECKING FOR NULL: SIMPLE APPROACH

Page 36: Fun with Kotlin

NULL SAFETY

b?.length

bob?.department?.head?.name

CHECKING FOR NULL: SAFE CALLS

Page 37: Fun with Kotlin

NULL SAFETY

CHECKING FOR NULL: ELVIS OPERATOR

Page 38: Fun with Kotlin

NULL SAFETY

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

val l = b?.length ?: -1

CHECKING FOR NULL: ELVIS OPERATOR

Page 39: Fun with Kotlin

NULL SAFETY

val l = b!!.length()

CHECKING FOR NULL: LIVING DANGEROUSLY

Page 40: Fun with Kotlin

FUNCTIONS

Page 41: Fun with Kotlin

FUNCTIONS

fun double(x: Int): Int { return x * 2}

val result = double(2)

FUNCTIONS IN KOTLIN

Page 42: Fun with Kotlin

FUNCTIONS

fun Int.times(n: Int) = this * n

println(2.times(3))

INFIX NOTATION

Page 43: Fun with Kotlin

FUNCTIONS

infix fun Int.times(n: Int) = this * n

println(2.times(3))

INFIX NOTATION

Page 44: Fun with Kotlin

FUNCTIONS

infix fun Int.times(n: Int) = this * n

println(2 times 3)

INFIX NOTATION

Page 45: Fun with Kotlin

FUNCTIONS

fun printString(str: String, prefix: String = "", postfix: String = "") { println(prefix + str + postfix)}

printString("hello")

printString("hello", "<")

printString("hello", "<", ">")

DEFAULT ARGUMENTS

Page 46: Fun with Kotlin

FUNCTIONS

fun printString(str: String, prefix: String = "", postfix: String = "") { println(prefix + str + postfix)}

printString(“hello”, prefix = “<“, postfix = “>”)

NAMED ARGUMENTS

Page 47: Fun with Kotlin

FUNCTIONS

fun <T> forEach(list: List<T>, f: (T) -> Unit) { for (elem in list) { f(elem) }}

val list = listOf("Alice", "Bob", "Claire")forEach(list, ::println)

forEach(list, { str -> println(str) })

HIGHER-ORDER FUNCTIONS

Page 48: Fun with Kotlin

FUNCTIONS

fun <T> forEach(list: List<T>, f: (T) -> Unit) { for (elem in list) { f(elem) }}

val list = listOf("Alice", "Bob", "Claire")forEach(list, ::println)

forEach(list, { println(it) })

HIGHER-ORDER FUNCTIONS

Page 49: Fun with Kotlin

FUNCTIONS

fun <T> forEach(list: List<T>, f: (T) -> Unit) { for (elem in list) { f(elem) }}

val list = listOf("Alice", "Bob", "Claire")forEach(list, ::println)

forEach(list) { println(it) }

HIGHER-ORDER FUNCTIONS

Page 50: Fun with Kotlin

SMART CASTS

Page 51: Fun with Kotlin

SMART CASTS

// Javaprivate static void demo(Object x) { if (x instanceof String) { String str = (String) x; System.out.println(str.length()); }}

CASTING IN JAVA

Page 52: Fun with Kotlin

SMART CASTS

// Javaprivate static void demo(Object x) { if (x instanceof String) { String str = (String) x; System.out.println(str.length()); }}

// Kotlinfun demo(x: Any) { if (x is String) { println(x.length) }}

SMART CASTS IN KOTLIN

Page 53: Fun with Kotlin

SMART CASTS

fun demo(x: Any) { if (x is String) { println(x.length) }}

IF EXPRESSION

Page 54: Fun with Kotlin

SMART CASTS

fun demo(x: Any) { if (x !is String) { println("Not a String") } else { println(x.length) }}

IF-ELSE EXPRESSION

Page 55: Fun with Kotlin

SMART CASTS

fun demo(x: Any) { if (x !is String) return println(x.length)}

RETURN STATEMENT

Page 56: Fun with Kotlin

SMART CASTS

if (x !is String || x.length == 0) return

if (x is String && x.length > 0) print(x.length)

BOOLEAN EXPRESSIONS

Page 57: Fun with Kotlin

WHAT’S NEXT?

Page 58: Fun with Kotlin

WHAT’S NEXT?

POST-1.0 ROADMAP

▸ New language features

▸ Java 8/9 support

▸ JavaScript support

▸ Android tooling improvements

▸ IDE features

Page 59: Fun with Kotlin

WHAT’S NEXT?

RESOURCES

▸ Kotlin - http://kotlinlang.org/

▸ Koans - http://try.kotlinlang.org/koans

▸ Blog - https://blog.jetbrains.com/kotlin/

▸ Forum - https://devnet.jetbrains.com/community/kotlin

▸ Slack - http://kotlinlang.slack.com/

Page 60: Fun with Kotlin

Thank you!@EgorAnd

+EgorAndreevichhttp://blog.egorand.me/