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

Kotlin

Drew Carlson
November 16, 2020
63

 Kotlin

Drew Carlson

November 16, 2020
Tweet

Transcript

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

    fun stringOrDefault( subject: String?, default: String = "Default" ): String { return subject "?: default }
  2. fun sum(a: Int, b: Int): Int = a + b

    fun stringOrDefault( subject: String?, default: String = "Default" ): String { return subject "?: default }
  3. fun sum(a: Int, b: Int): Int = a + b

    fun stringOrDefault( subject: String?, default: String = "Default" ): String { return subject "?: default }
  4. fun sum(a: Int, b: Int): Int = a + b

    fun stringOrDefault( subject: String?, default: String = "Default" ): String { return subject "?: default }
  5. fun sum(a: Int, b: Int): Int = a + b

    fun stringOrDefault( subject: String?, default: String = "Default" ): String { return subject "?: default } stringOrDefault(name)
  6. fun sum(a: Int, b: Int): Int = a + b

    fun stringOrDefault( subject: String?, default: String = "Default" ): String { return subject "?: default } stringOrDefault(name) stringOrDefault(name, "missing")
  7. "// Java public class User { @NonNull public String getName()

    { "// ""... } public void setName(@NonNull String name) { "// ""... } }
  8. "// Kotlin val user = User() println("User: " + user.name)

    "// Java public class User { @NonNull public String getName() { "// ""... } public void setName(@NonNull String name) { "// ""... } }
  9. "// Kotlin val user = User() println("User: " + user.name)

    "// Java public class User { @NonNull public String getName() { "// ""... } public void setName(@NonNull String name) { "// ""... } }
  10. "// Kotlin val user = User() println("User: " + user.name)

    user.name = "Not Drew" "// Java public class User { @NonNull public String getName() { "// ""... } public void setName(@NonNull String name) { "// ""... } }
  11. "// Kotlin val user = User() println("User: " + user.name)

    user.name = "Not Drew" "// Java public class User { @NonNull public String getName() { "// ""... } public void setName(@NonNull String name) { "// ""... } }
  12. "// Kotlin val user = User() println("User: ${user.name}") user.name =

    "Not Drew" "// Java public class User { @NonNull public String getName() { "// ""... } public void setName(@NonNull String name) { "// ""... } }
  13. "// Kotlin val user = User() println("User: $user") user.name =

    "Not Drew" "// Java public class User { @NonNull public String getName() { "// ""... } public void setName(@NonNull String name) { "// ""... } }
  14. "// Kotlin val user = User() println("User: $user") user.name =

    "Not Drew" "// Java public class User { @NonNull public String getName() { "// ""... } public void setName(@NonNull String name) { "// ""... } }
  15. "// Java final User user = new User(); System.out.println("User: "

    + user.getName()); "// Kotlin class User { var name = "Drew" }
  16. "// Java final User user = new User(); System.out.println("User: "

    + user.getName()); "// Kotlin class User { var name = "Drew" }
  17. "// Java final User user = new User(); System.out.println("User: "

    + user.getName()); user.setName("Not Drew"); "// Kotlin class User { var name = "Drew" }
  18. "// Java final User user = new User(); System.out.println("User: "

    + user.getName()); user.setName("Not Drew"); "// Kotlin class User { var name = "Drew" }
  19. "// Kotlin "// file: com/app/locale.kt fun Locale.isLanguageEnglish(): Boolean { return

    language "== Locale.ENGLISH.language } val locale = Locale.GERMANY if (locale.isLanguageEnglish()) { println("The Locale's language is English.") } else { println("The Locale's language is not english") }
  20. "// Kotlin "// file: com/app/locale.kt fun Locale.isLanguageEnglish(): Boolean { return

    language "== Locale.ENGLISH.language } val locale = Locale.GERMANY if (locale.isLanguageEnglish()) { println("The Locale's language is English.") } else { println("The Locale's language is not english") }
  21. "// Kotlin "// file: com/app/locale.kt fun Locale.isLanguageEnglish(): Boolean { return

    language "== Locale.ENGLISH.language } import com.app.isLanguageEnglish val locale = Locale.GERMANY if (locale.isLanguageEnglish()) { println("The Locale's language is English.") } else { println("The Locale's language is not english") }
  22. "// Java LocaleKt.isLanguageEnglish(Locale.UK); val locale = Locale.GERMANY if (locale.isLanguageEnglish()) {

    "//""... } "// Kotlin "// file: com/app/locale.kt fun Locale.isLanguageEnglish(): Boolean { return language "== Locale.ENGLISH.language }
  23. "// Kotlin "// file: com/app/locale.kt @file:JvmName("LocaleUtils") fun Locale.isLanguageEnglish(): Boolean {

    return language "== Locale.ENGLISH.language } val locale = Locale.GERMANY if (locale.isLanguageEnglish()) { "//""... } "// Java LocaleKt.isLanguageEnglish(Locale.UK);
  24. "// Kotlin "// file: com/app/locale.kt @file:JvmName("LocaleUtils") fun Locale.isLanguageEnglish(): Boolean {

    return language "== Locale.ENGLISH.language } val locale = Locale.GERMANY if (locale.isLanguageEnglish()) { "//""... } "// Java LocaleUtils.isLanguageEnglish(Locale.UK);
  25. val work = { println("from the background") } val executor

    = Executors.newCachedThreadPool() executor.execute(work)
  26. val work = { println("from the background") } val executor

    = Executors.newCachedThreadPool() executor.execute(work)
  27. fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { "// ""...

    } val names = listOf("Drew", "Ahsan", "Shivangi") val drews = names.filter({ name !-> name "== "Drew" })
  28. fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { "// ""...

    } val drews = names.filter({ name !-> name "== "Drew" }) val names = listOf("Drew", "Ahsan", "Shivangi")
  29. fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { "// ""...

    } val drews = names.filter({ it "== "Drew" }) val names = listOf("Drew", "Ahsan", "Shivangi")
  30. fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { "// ""...

    } val drews = names.filter() { it "== "Drew" } val names = listOf("Drew", "Ahsan", "Shivangi")
  31. fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { "// ""...

    } val drews = names.filter { it "== "Drew" } val names = listOf("Drew", "Ahsan", "Shivangi")
  32. fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { "// ""...

    } val drews = names.filter { it "== "Drew" } val names = listOf("Drew", "Ahsan", "Shivangi") val ahsans = names.filterTo(mutableSetOf()) { it == "Ahsan" }
  33. fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { "// ""...

    } val drews = names.filter { it "== "Drew" } val names = listOf("Drew", "Ahsan", "Shivangi") val ahsans = names.filterTo(mutableSetOf()) { it == "Ahsan" }
  34. fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { "// ""...

    } val drews = names.filter { it "== "Drew" } val names = listOf("Drew", "Ahsan", "Shivangi") val ahsans = names.filterTo(mutableSetOf()) { it == "Ahsan" }
  35. val drews = names.filter { it "== "Drew" } val

    names = listOf("Drew", "Ahsan", "Shivangi") fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { "// ""... }
  36. val drews = names.filter { it "== "Drew" } val

    names = listOf("Drew", "Ahsan", "Shivangi") inline fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { "// ""... }
  37. inline fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { val

    destination = mutableListOf<T>() for (item in this) { if (predicate(item)) destination.add(item) } return destination } val names = listOf("Drew", "Ahsan", "Shivangi") val drews = names.filter { it "== "Drew" }
  38. inline fun <T> List<T>.filter(predicate: (T) "-> Boolean): List<T> { val

    destination = mutableListOf<T>() for (item in this) { if (predicate(item)) destination.add(item) } return destination } val destination = mutableListOf<String>() for (item in names) { if (item "== "Drew") destination.add(item) } val drews: List<String> = destination
  39. data class User(val name: String, val age: Int) val drew

    = User("Drew", 5) val updated = drew.copy(age = 6)
  40. data class User(val name: String, val age: Int) val drew

    = User("Drew", 5) val (name, age) = drew
  41. data class User(val name: String, val age: Int) val drew

    = User("Drew", 5) val (name, age) = drew val (key, value) = Pair("session_id", "1")
  42. data class User(val name: String, val age: Int) val drew

    = User("Drew", 5) val (name, age) = drew val (key, value) = Pair("session_id", "1") val (x, y, z) = Triple(0f, 0f, 1f)
  43. data class User(val name: String, val age: Int) val drew

    = User("Drew", 5) val (name, age) = drew val (key, value) = Pair("session_id", "1") val (x, y, z) = Triple(0f, 0f, 1f)
  44. // otherfile.kt class ExternalResult<out T : Any> : Result<T>() sealed

    class Result<T> { class Success<T> : Result<T>() class Error : Result<Nothing>() }
  45. sealed class Result<T> { data class Success<T>(val data: T) :

    Result<T>() data class Error(val exception: Exception) : Result<Nothing>() } // otherfile.kt class ExternalResult<out T : Any> : Result<T>()
  46. sealed class Result<T> { data class Success<T>(val data: T) :

    Result<T>() data class Error(val exception: Exception) : Result<Nothing>() }
  47. sealed class Result<T> { data class Success<T>(val data: T) :

    Result<T>() data class Error(val exception: Exception) : Result<Nothing>() } fun handleResult(result: Result<String>) { when (result) { is Result.Success -> TODO("handle") is Result.Error -> TODO("handle") } }
  48. sealed class Result<T> { data class Success<T>(val data: T) :

    Result<T>() data class Error(val exception: Exception) : Result<Nothing>() } fun handleResult(result: Result<String>) { when (result) { is Result.Success -> result.data is Result.Error -> result.exception.message } }
  49. sealed class Result<T> { data class Success<T>(val data: T) :

    Result<T>() data class Error(val exception: Exception) : Result<Nothing>() } fun handleResult(result: Result<String>): String { return when (result) { is Result.Success -> result.data is Result.Error -> result.exception.message } }
  50. sealed class Result<T> { data class Success<T>(val data: T) :

    Result<T>() data class Error(val exception: Exception) : Result<Nothing>() object Cancelled : Result<Nothing>() } fun handleResult(result: Result<String>): String { return when (result) { is Result.Success -> result.data is Result.Error -> result.exception.message } }
  51. fun handleResult(result: Result<String>): String { return when (result) { is

    Result.Success -> result.data is Result.Error -> result.exception.message Result.Cancelled -> "cancelled" } } sealed class Result<T> { data class Success<T>(val data: T) : Result<T>() data class Error(val exception: Exception) : Result<Nothing>() object Cancelled : Result<Nothing>() }
  52. sealed class Result<T> { data class Success<T>(val data: T) :

    Result<T>() data class Error(val exception: Exception) : Result<Nothing>() object Cancelled : Result<Nothing>() } fun handleResult(result: Result<String>): String { return when (result) { is Result.Success -> result.data is Result.Error -> result.exception.message Result.Cancelled -> "cancelled" } }
  53. sealed class Result<T> data class Success<T>(val data: T) : Result<T>()

    data class Error(val exception: Exception) : Result<Nothing>() object Cancelled : Result<Nothing>() fun handleResult(result: Result<String>): String { return when (result) { is Success -> result.data is Error -> result.exception.message Cancelled -> "cancelled" } }
  54. class Preferences(databaseName: String) { private val db = SqlDatabase(databaseName) fun

    delete(name: String) { db.beginTransaction() try { db.execute("DELETE FROM user WHERE name = ?”) db.setTransactionSuccessful() } finally { db.endTransaction() } } }
  55. class Preferences(databaseName: String) { private val db = SqlDatabase(databaseName) fun

    delete(name: String) { db.beginTransaction() try { db.execute("DELETE FROM user WHERE name = ?”) db.setTransactionSuccessful() } finally { db.endTransaction() } } }
  56. class Preferences(databaseName: String) { private val db by lazy {

    SqlDatabase(databaseName) } fun delete(name: String) { db.beginTransaction() try { db.execute("DELETE FROM user WHERE name = ?”) db.setTransactionSuccessful() } finally { db.endTransaction() } } }
  57. private val db by lazy { SqlDatabase(databaseName) } var name

    by Delegates.observable("Drew") { prop, old, new !-> println("Name changed from $old to $new") }
  58. private val db by lazy { SqlDatabase(databaseName) } var name

    by Delegates.observable("Drew") { prop, old, new !-> println("Name changed from $old to $new") } var address by Delegates.notNull<String>()
  59. private val db by lazy { SqlDatabase(databaseName) } var name

    by Delegates.observable("Drew") { prop, old, new !-> println("Name changed from $old to $new") } var address by Delegates.notNull<String>() val labelView by bindView<TextView>(R.id.label)
  60. class Preferences(databaseName: String) { private val db by lazy {

    SqlDatabase(databaseName) } fun delete(name: String) { db.beginTransaction() try { db.execute("DELETE FROM user WHERE name = ?”) db.setTransactionSuccessful() } finally { db.endTransaction() } } } var name by Delegates.observable("Drew") { prop, old, new !-> println("Name changed from $old to $new") } var address by Delegates.notNull<String>() val labelView by bindView<TextView>(R.id.label)
  61. class Preferences(databaseName: String) { private val db by lazy {

    SqlDatabase(databaseName) } fun delete(name: String) { db.beginTransaction() try { db.execute("DELETE FROM user WHERE name = ?”) db.setTransactionSuccessful() } finally { db.endTransaction() } } } val labelView by bindView<TextView>(R.id.label)
  62. fun delete(name: String) { db.beginTransaction() try { db.execute("DELETE FROM user

    WHERE name = ?") db.setTransactionSuccessful() } finally { db.endTransaction() } } class Preferences(databaseName: String) { private val db by lazy { SplDatabase(databaseName) } }
  63. fun SqlDatabase.transaction(func: () "-> Unit) { beginTransaction() try { func()

    setTransactionSuccessful() } finally { endTransaction() } }
  64. fun SqlDatabase.transaction(func: () "-> Unit) { beginTransaction() try { func()

    setTransactionSuccessful() } finally { endTransaction() } }
  65. fun SqlDatabase.transaction(func: () "-> Unit) { beginTransaction() try { func()

    setTransactionSuccessful() } finally { endTransaction() } }
  66. fun SqlDatabase.transaction(func: () "-> Unit) { beginTransaction() try { func()

    setTransactionSuccessful() } finally { endTransaction() } }
  67. fun SqlDatabase.transaction(func: () "-> Unit) { beginTransaction() try { func()

    setTransactionSuccessful() } finally { endTransaction() } }
  68. fun SqlDatabase.transaction(func: () "-> Unit) { beginTransaction() try { func()

    setTransactionSuccessful() } finally { endTransaction() } } fun delete(name: String) { db.transaction { db.execute("DELETE FROM user WHERE name = ?") } }
  69. fun SqlDatabase.transaction(func: () "-> Unit) { beginTransaction() try { func()

    setTransactionSuccessful() } finally { endTransaction() } } fun delete(name: String) { db.transaction { db.execute("DELETE FROM user WHERE name = ?") } }
  70. fun SqlDatabase.transaction(func: (SqlDatabase) "-> Unit) { beginTransaction() try { func()

    setTransactionSuccessful() } finally { endTransaction() } } fun delete(name: String) { db.transaction { db.execute("DELETE FROM user WHERE name = ?") } } .execute("DELETE FROM user WHERE name = ?")
  71. fun SqlDatabase.transaction(func: (SqlDatabase) "-> Unit) { beginTransaction() try { func(this)

    setTransactionSuccessful() } finally { endTransaction() } } fun delete(name: String) { db.transaction { db.execute("DELETE FROM user WHERE name = ?") } } .execute("DELETE FROM user WHERE name = ?")
  72. fun SqlDatabase.transaction(func: (SqlDatabase) "-> Unit) { beginTransaction() try { func(this)

    setTransactionSuccessful() } finally { endTransaction() } } fun delete(name: String) { db.transaction { it.execute("DELETE FROM user WHERE name = ?") } } .execute("DELETE FROM user WHERE name = ?")
  73. fun SqlDatabase.transaction(func: (SqlDatabase) "-> Unit) { beginTransaction() try { func(this)

    setTransactionSuccessful() } finally { endTransaction() } } fun delete(name: String) { db.transaction { it.execute("DELETE FROM user WHERE name = ?") } } .execute("DELETE FROM user WHERE name = ?")
  74. fun delete(name: String) { db.transaction { it.execute("DELETE FROM user WHERE

    name = ?") } } fun SqlDatabase.transaction(func: SqlDatabase.() "-> Unit) { beginTransaction() try { func(this) setTransactionSuccessful() } finally { endTransaction() } }
  75. fun delete(name: String) { db.transaction { it.execute("DELETE FROM user WHERE

    name = ?") } } fun SqlDatabase.transaction(func: SqlDatabase.() "-> Unit) { beginTransaction() try { this.func() setTransactionSuccessful() } finally { endTransaction() } }
  76. fun delete(name: String) { db.transaction { it.execute("DELETE FROM user WHERE

    name = ?") } } fun SqlDatabase.transaction(func: SqlDatabase.() "-> Unit) { beginTransaction() try { func() setTransactionSuccessful() } finally { endTransaction() } }
  77. fun delete(name: String) { db.transaction { execute("DELETE FROM user WHERE

    name = ?") } } fun SqlDatabase.transaction(func: SqlDatabase.() "-> Unit) { beginTransaction() try { func() setTransactionSuccessful() } finally { endTransaction() } }
  78. @Serializable data class User(val name: String, val age: Int) val

    drew = User("Drew", 5) val jsonString = Json.encodeToString(drew)
  79. @Serializable data class User(val name: String, val age: Int) val

    drew = User("Drew", 5) val jsonString = Json.encodeToString(drew) println(jsonString) "// {"name":"Drew","age":5}
  80. @Serializable data class User(val name: String, val age: Int) val

    drew = User("Drew", 5) val jsonString = Json.encodeToString(drew) println(jsonString) "// {"name":"Drew","age":5} val drew = Json.decodeFromString<User>(jsonString)
  81. @Serializable data class User(val name: String, val age: Int) val

    drew = User("Drew", 5) val jsonString = Json.encodeToString(drew) println(jsonString) "// {"name":"Drew","age":5} val drew = Json.decodeFromString<User>(jsonString) println(drew) "// User(name=Drew, age=5)
  82. @Serializable data class User(val name: String, val age: Int) val

    drew = User("Drew", 5) val jsonString = Json.encodeToString(drew) println(jsonString) "// {"name":"Drew","age":5} val drew = Json.decodeFromString<User>(jsonString) println(drew) "// User(name=Drew, age=5)
  83. fun main() = runBlocking { repeat(100_000) { launch { delayAndPrint()

    } } } suspend fun delayAndPrint() { delay(1.seconds) print(".") }
  84. fun main() = runBlocking { repeat(100_000) { launch { delayAndPrint()

    } } } suspend fun delayAndPrint() { delay(1.seconds) print(".") }
  85. fun main() = runBlocking { repeat(100_000) { launch { delayAndPrint()

    } } } suspend fun delayAndPrint() { delay(1.seconds) print(".") }
  86. suspend fun updateUserName() { Main { val user = Default

    { http.get("/api/user/1") } labelName.text = user.name } }
  87. suspend fun updateUserName() { Main { val user = Default

    { http.get("/api/user/1") } labelName.text = user.name } }
  88. suspend fun updateUserName() { Main { val user = Default

    { http.get("/api/user/1") } labelName.text = user.name } }
  89. suspend fun updateUserName() { Main { val user = Default

    { http.get("/api/user/1") } labelName.text = user.name } }
  90. suspend fun updateUserName() { Main { val user = Default

    { http.get("/api/user/1") } labelName.text = user.name } }
  91. suspend fun updateUserName() { Main { val user = Default

    { http.get("/api/user/1") } labelName.text = user.name } }
  92. suspend fun updateUserName() { Main { val user = try

    { Default { http.get("/api/user/1") } } catch (e: HttpResponseException) { e.printStacktrace() null } labelName.text = user"?.name } }
  93. suspend fun updateUserName() { Main { val user = try

    { Default { http.get("/api/user/1") } } catch (e: HttpResponseException) { e.printStacktrace() null } labelName.text = user"?.name } }