No excuses, switch to kotlin

30
NO EXCUSES, SWITCH TO KOTLIN

Transcript of No excuses, switch to kotlin

NO EXCUSES, SWITCH TO KOTLIN

NO EXCUSES, SWITCH TO KOTLIN

t

Thijs Suijten Mobile developer @ Q42

@tsuijten

GETTING EVERYBODY ONBOARD

TEAM Q42 CLIENT

val s1: String = "Hello" // Immutable variableval s2 = "how are you" // Type is inferred

Variables

var s3 = "John" // Mutable variables3 = "Thijs" // Assign a different value

// w00t! String Interpolationprintln("$s1, $s2 $s3?") //> Hello, how are you Thijs?

fun reformat(str: String, normalize: Boolean = true, upperCase: Boolean = true): String { ...}

Functions

reformat("Hello world") reformat("Hello world", false, false) reformat("Hello world", upperCase = false)

// Single-Expression functions fun twiceTheFun(x: Int) = x * 2

class Person(val firstName: String, val age: Int, val locale: Locale = ENGLISH) { fun sayHello() = println("Hi $firstName!") }

Classes

val thijs = Person("Thijs", 34) thijs.sayHello() // Hi Thijs!

class Cat(name: String) { val name: String = name}

val x: String? = getSomeOptionalString()x.length // Does not compile.

Null safety / optionals

val y: String = null // Does not compile.

if (x != null) { x.length // Compiles!}

// Safe callval optLength = foo?.bar?.length // Returns Int?// Elvis operator.val length = optLength ?: -1

Data classes

class User(val name: String, val age: Int)

val thijs = User("Thijs", 35) println(thijs) // User(name=Thijs, age=35)

val bday = thijs.copy(age = 36) // Named params!println(bday) // User(name=Thijs, age=36)

data

Data classespublic class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (age != user.age) return false; return name != null ? name.equals(user.name) : user.name == null; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }

// Remember this?data class User(val name: String, val age: Int)

public class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (age != user.age) return false; return name != null ? name.equals(user.name) : user.name == null; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }

Functional / lambas

val sum = listOf(1, 2, 3, 4, 5) // 1,2,3,4,5.filter { number -> number < 5 } // 1,2,3,4.map { it * 2 } // 2,4,6,8.sum() // 20

listOf("Zack", "Bob", "Thijs", "Julie", "Rob") .filter { it.length > 3 } // Zack, Thijs, Julie.sorted() // Julie, Thijs, Zack

Collections

// Interface List<String>val fruits = listOf("Apple", "Pear", "Orange") fruits.add("Strawberry") // Compiler error

// Interface MutableList<String>val veggies = mutableListOf("Kale", "Spinach", "Lettuce") veggies.add("Tomato") veggies += "Cucumber" // Operator overloading

When statement// Javaif (firstName.equals("Dan")) { person.setTeam(programmers); } else if (lastName.equals("Jamie")) { person.setTeam(designers); } else { person.setTeam(others); }

// Kotlinwhen { firstName == “Dan" -> person.team = programmers lastName == "Jamie" -> person.team = designers else -> person.team = others}

When statement// Javaswitch (firstName) { case "Dan": case "Jay": person.setTeam(programmers); break; case "Jamie": person.setTeam(designers); break; default: person.setTeam(others); }

// Kotlinwhen (firstName) { "Dan", "Jay" -> person.team = programmers "Jamie" -> person.team = designers else -> person.team = others}

When statementwhen (x) { 1 -> print("x = 1") is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) in 10..20 -> print("Between 10 and 20") }

val outcome = when(x) { 1 -> "x = 1" "Hello" -> "is it me you're looking for?" in 10..20 -> "Between 10 and 20"}

Extensions

// JavaStringUtils.capitalize(AddressUtils.extractStreet(address))

// Kotlinaddress.street.capitalize()

// Extension functionfun String.capitalize() = StringUtils.capitalize(this) // Extension propertyval String.street: String get() = AddressUtils.extractStreet(this)

Kotlin Android extensions

<TextView android:id="@+id/greeting" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me!"/>

override fun onCreate(savedInstanceState: Bundle?) { //… greeting.text = "Hello" button.onClick { toast("Button clicked!") } }

Interop Demo

Who is using Kotlin

NO EXCUSES, SWITCH TO KOTLIN

..

[email protected] / @tsuijten try.kotlinlang.org