Upgrade to Pro — share decks privately, control downloads, hide ads and more …

kotlin for android developers

Karumi
November 10, 2018

kotlin for android developers

It's an introduction to android but fixing common problems that Android developers have and how to resolve it using kotlin. it's not another kotlin introduction talk.

Karumi

November 10, 2018
Tweet

More Decks by Karumi

Other Decks in Programming

Transcript

  1. Testing for android & iOS. Trainings Architecture, Patterns and principles

    for Android & iOS. Mastering Git. Advanced mobile development. Testing for android & iOS. Architecture, Patterns and principles for Android & iOS. Companies For Everybody Next open: Testing Training 23 Jun. Kotlin Training 20 Feb. Swift Training 26 Mar. Arch Training 3 Apr.
  2. Adam Tornhill “Computer languages differ not so much in what

    they make possible, but in what they make easy.” Larry Wall
  3. val email: String? = user?.email Kotlin Is user == null?

    email is null and the property is never accessed Then email is the user email (that… can be null!) YES NO
  4. val email: String? = user?.email ?: "[email protected]" Is user?.email ==

    null? Then email is “[email protected]" Then email is the user email YES NO Kotlin Elvis operator
  5. val email: String = user!!.email Kotlin Force cast Is user

    == null? Then NullPointerException is thrown Then email is the user email YES NO
  6. listOf( User(name = “Jane”, age = 45), User(name = “Joe”,

    age = 23, email = “[email protected]”), User(name = “Jan”, age = 19, email = “[email protected]"), User(name = “J.D.”, age = 66) ).filter { it.email != null } .map {it.email} // [“[email protected]”,"[email protected]"] Kotlin
  7. fun doSomethingUsefulOne(): Int { delay(1000L) return 13 } fun doSomethingUsefulTwo():

    Int { delay(1000L) return 29 } val time = measureTimeMillis { val one = doSomethingUsefulOne() val two = doSomethingUsefulTwo() println("${one + two}") } println("$time ms”) [“42”, “time 2017”] Kotlin Corrutines suspend fun doSomethingUsefulOne(): Int { delay(1000L) return 13 } suspend fun doSomethingUsefulTwo(): Int { delay(1000L) return 29 } val time = measureTimeMillis { val one = async { doSomethingUsefulOne() } val two = async { doSomethingUsefulTwo() } println("${one.await() + two.await()}") } println("$time ms") [“42”, “time 1017”]
  8. val user: Either<Error,User> = repository.getUserById(id); when(user) { is Right ->

    showUser(it) is Left -> showError(getMessage(it)) } Kotlin val user: Either<Error,User> = repository.getUserById(id); user.fold( isLeft = {showUser(it)}, isRight= {showError(getMessage(it))});
  9. fun getMessage(domainError: Error): Int = when (domainError) { is NotInternetDomainError

    -> R.string.error_not_internet_message is NotIndexFoundDomainError -> R.string.error_superhero_not_found_message is UnknownDomainError -> R.string.error_unknown_message } Kotlin sealed class DomainError object NotInternetDomainError : DomainError() data class UnknownDomainError( val errorMessage: String = "Unknown Error") : DomainError() data class NotIndexFoundDomainError(val key: String) : DomainError() Compile error!!