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

Kotlin, beyond the basics

Kotlin, beyond the basics

Talk given at 360AnDev (https://360andev.com/) 2018 in Denver Colorado, USA.

Video: https://www.youtube.com/watch?v=Ot2DOKztu38

Features 3 important topics for intermediate Kotlin developers - adapting to the functional programming paradigm, learning more about functions and lambdas, and learning about Kotlin-Java interoperability

Segun Famisa

July 20, 2018
Tweet

More Decks by Segun Famisa

Other Decks in Programming

Transcript

  1. Functional == actions are composed into functions and usually reads

    as “what is done” Imperative == structured in steps of execution and usually reads as “how it’s done” Imperative == structured in steps of execution and usually reads as “how it’s done”
  2. Functional == actions are composed into functions and usually reads

    as “what is done” Imperative == structured in steps of execution and usually reads as “how it’s done” Functional == actions are composed into functions and usually reads as “what is done” Imperative == structured in steps of execution and usually reads as “how it’s done”
  3. // Java - print even numbers for (int i =

    start; i < end; i++) { if (i % 2 == 0) { System.out.println(i); } }
  4. // Java - print even numbers for (int i =

    start; i < end; i++) { if (i % 2 == 0) { System.out.println(i); } } // Kotlin - print even numbers (start until end) .filter { it % 2 == 0 } .map { println(it) }
  5. Imperative Functional Immutability isn’t encouraged at language level Encourages immutability

    Instances of classes, structures, objects are first class citizens Functions are first class citizens
  6. Imperative Functional Immutability isn’t encouraged at language level Encourages immutability

    Instances of classes, structures, objects are first class citizens Functions are first class citizens Loops, method calls, conditionals Function calls
  7. Imperative Functional Immutability isn’t encouraged at language level Encourages immutability

    Instances of classes, structures, objects are first class citizens Functions are first class citizens Loops, method calls, conditionals Function calls Side-effects allowed in functions Pure functions
  8. Functions are first class citizens They can: • be stored

    in a variable - just like other types
  9. Functions are first class citizens They can: • be stored

    in a variable - just like other types • take another function as parameter
  10. Functions are first class citizens They can: • be stored

    in a variable - just like other types • take another function as parameter • return a function
  11. Functions are first class citizens They can: • be stored

    in a variable - just like other types • take another function as parameter • return a function a.k.a higher order functions
  12. Lambdas & higher order functions fun atLeastAndroidP(action: () -> Unit)

    { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { action() } }
  13. Lambdas & higher order functions // Usage: atLeastAndroidP { //

    do something on Android P } fun atLeastAndroidP(action: () -> Unit) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { action() } }
  14. Lambdas & higher order functions fun atLeastAndroidP(action: () -> Unit)

    { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { action() } } Lambda // Usage: atLeastAndroidP { // do something on Android P }
  15. Lambdas & higher order functions fun atLeastAndroidP(action: () -> Unit)

    { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { action() } } Higher order function // Usage: atLeastAndroidP { // do something on Android P }
  16. Lambdas & higher order functions // Usage: atLeastAndroidP { //

    do something on Android P } fun atLeastAndroidP(action: () -> Unit) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { action() } }
  17. Lambdas & higher order functions // Usage: atLeastAndroidP { //

    do something on Android P } fun atLeastAndroidP(action: () -> Unit) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { action() } } Tip: Take a look at the generated code to understand what’s happening under the hood
  18. Lambdas & higher order functions // Generated Java code public

    final void atLeastAndroidP(@NotNull Function0 action) { if (VERSION.SDK_INT > 28) { action.invoke(); } } fun atLeastAndroidP(action: () -> Unit) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { action() } }
  19. Lambdas & higher order functions // Generated Java code public

    final void atLeastAndroidP(@NotNull Function0 action) { if (VERSION.SDK_INT > 28) { action.invoke(); } } Every lambda is an object! Not great for performance fun atLeastAndroidP(action: () -> Unit) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { action() } }
  20. () -> Unit Function0<Unit> (Int) -> Boolean Function1<Int,Boolean> (Int, String)

    -> Boolean Function2<Int, String, Boolean> Every lambda corresponds to a FunctionN interface - where N is the number of params in the lambda
  21. Lambdas & higher order functions // FunctionN interfaces used by

    Kotlin interface Function0<out R> : Function<R> interface Function1<in P1, out R> : Function<R> interface Function2<in P1, in P2, out R> : Function<R> ... interface Function22<in P1, in P2...in P22, out R> : Function<R> { /** Invokes the function with the specified arguments. */ public operator fun invoke(p1: P1, p2: P2...p22: P22): R }
  22. Improving lambdas’ performance - the inline keyword // Kotlin inline

    fun atLeastAndroidP(action: () -> Unit) {...}
  23. Improving lambdas’ performance - the inline keyword // Kotlin inline

    fun atLeastAndroidP(action: () -> Unit) {...} // Usage: fun doSomethingOnP() { atLeastAndroidP { println("I'm on Android P") } }
  24. Improving lambdas’ performance - the inline keyword // Kotlin inline

    fun atLeastAndroidP(action: () -> Unit) {...} // Usage: fun doSomethingOnP() { atLeastAndroidP { println("I'm on Android P") } } // Generated Java code public final void doSomethingOnP() { if (VERSION.SDK_INT > 28) { String var0 = "I'm on Android P"; System.out.println(var0); } }
  25. Improving lambdas’ performance - the inline keyword // Kotlin inline

    fun atLeastAndroidP(action: () -> Unit) {...} // Usage: fun doSomethingOnP() { atLeastAndroidP { println("I'm on Android P") } } // Generated Java code public final void doSomethingOnP() { if (VERSION.SDK_INT > 28) { String var0 = "I'm on Android P"; System.out.println(var0); } } Method body is copied to the call site
  26. noinline keyword - used to mark lambdas in an inline

    function that we don’t want to inline Other good-to-know concepts when working with lambdas:
  27. noinline keyword - used to mark lambdas in an inline

    function that we don’t want to inline Other good-to-know concepts when working with lambdas: inline fun atLeastAndroidP(action: () -> Unit, noinline fallback: () -> Unit) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) { action() } else { fallback() } }
  28. noinline keyword - used to mark other lambda params in

    an inline function that we don’t want to inline Other good-to-know concepts when working with lambdas: inline fun atLeastAndroidP(action: () -> Unit, noinline fallback: () -> Unit) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) { action() } else { fallback() } } the `fallback` lambda is not inlined
  29. non-local returns - typically non-local returns are not allowed within

    lambdas unless the function is inlined. Other good-to-know concepts when working with lambdas:
  30. non-local returns - typically non-local returns are not allowed within

    lambdas unless the function is inlined. fun useNormalLambda() { normalLambda { ... return@normalLambda // works fine (local return) } } Other good-to-know concepts when working with lambdas:
  31. non-local returns - typically non-local returns are not allowed within

    lambdas unless the function is inlined. fun useNormalLambda() { normalLambda { ... return@normalLambda // works fine (local return) return // compile error (non-local return) } } Other good-to-know concepts when working with lambdas:
  32. non-local returns - typically non-local returns are not allowed within

    lambdas unless the function is inlined. Other good-to-know concepts when working with lambdas: fun useNormalLambda() { normalLambda { ... return@normalLambda // works fine (local return) return // compile error (non-local return } } fun useInlineLambda() { inlineLambda { return // works fine. Returns from useInlineLambda } }
  33. crossinline keyword - used when the lambdas are not used

    directly in the inline function Other good-to-know concepts when working with lambdas:
  34. crossinline keyword - used when the lambdas are not used

    directly in the inline function Other good-to-know concepts when working with lambdas: inline fun View.waitForLayout(crossinline action: () -> Unit) = with(viewTreeObserver) { addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { [email protected](this) action() } }) }
  35. crossinline keyword - used when the lambdas are not used

    directly in the inline function Other good-to-know concepts when working with lambdas: inline fun View.waitForLayout(crossinline action: () -> Unit) = with(viewTreeObserver) { addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { [email protected](this) action() } }) } Non-local returns are not allowed within the lambda
  36. let - Standard.kt public inline fun <T, R> T.let(block: (T)

    -> R): R = block(this) Lambda function
  37. let - Standard.kt public inline fun <T, R> T.let(block: (T)

    -> R): R = block(this) // Sample usage client?.email ?.let { mailer.sendMessage(it, message) }
  38. let - Standard.kt // Sample usage client?.email ?.let { mailer.sendMessage(it,

    message) } public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
  39. let - Standard.kt // Sample usage client?.email ?.let { mailer.sendMessage(it,

    message) } // Or client?.email ?.let { email -> mailer.sendMessage(email, message) } public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
  40. run - Standard.kt public inline fun <T, R> T.run(block: T.()

    -> R): R = block() Runs the block of code and returns the last line in the lambda
  41. run - Standard.kt public inline fun <T, R> T.run(block: T.()

    -> R): R = block() Function literal with receiver
  42. run - Standard.kt public inline fun <T, R> T.run(block: T.()

    -> R): R = block() // Sample usage fun performTransformation(input: String) : String {...} val transformedName = name.run { println("Transforming name...") val transformedString = performTransformation(this) transformedString }
  43. run - Standard.kt public inline fun <T, R> T.run(block: T.()

    -> R): R = block() // Sample usage fun performTransformation(input: String) : String {...} val transformedName = name.run { println("Transforming name...") val transformedString = performTransformation(this) transformedString }
  44. run - Standard.kt public inline fun <T, R> T.run(block: T.()

    -> R): R = block() // Sample usage fun performTransformation(input: String) : String {...} val transformedName = name.run { println("Transforming name...") val transformedString = performTransformation(this) transformedString }
  45. run - Standard.kt public inline fun <T, R> T.run(block: T.()

    -> R): R = block() // Sample usage fun performTransformation(input: String) : String {...} val transformedName = name.run { println("Transforming name...") val transformedString = performTransformation(this) transformedString } Receiver is accessed as “this”, and exposes the inner members of the receiver class
  46. run - Standard.kt public inline fun <T, R> T.run(block: T.()

    -> R): R = block() // Sample usage fun performTransformation(input: String) : String {...} val transformedName = name.run { println("Transforming name...") val transformedString = performTransformation(this) transformedString } transformedString is returned from the lambda
  47. also - Standard.kt inline fun <T> T.also(block: (T) -> Unit):

    T { block(this); return this } // Sample usage val dev = Developer().also { it.name = "Segun" it.lovesCatVideos = true it.stack = "Android" }
  48. also - Standard.kt inline fun <T> T.also(block: (T) -> Unit):

    T { block(this); return this } The receiver is accessed as “it” // Sample usage val dev = Developer().also { it.name = "Segun" it.lovesCatVideos = true it.stack = "Android" }
  49. also - Standard.kt inline fun <T> T.also(block: (T) -> Unit):

    T { block(this); return this } // Sample usage val dev = Developer().also { it.name = "Segun" it.lovesCatVideos = true it.stack = "Android" } // OR val dev = Developer().also { developer -> developer.name = "Segun" developer.lovesCatVideos = true developer.stack = "Android" }
  50. apply - Standard.kt inline fun <T> T.apply(block: T.() -> Unit):

    T { block(); return this } // Sample usage val dev = Developer().apply { this.name = "Segun" this.lovesCatVideos = true this.stack = "Android" }
  51. apply - Standard.kt inline fun <T> T.apply(block: T.() -> Unit):

    T { block(); return this } // Sample usage val dev = Developer().apply { this.name = "Segun" this.lovesCatVideos = true this.stack = "Android" } Exposes the receiver as “this”
  52. Things to consider • Avoid overusing these scoping functions -

    readability can degrade quickly • Consider renaming it to something better when possible
  53. Things to consider • Avoid overusing these scoping functions -

    readability can degrade quickly • Consider renaming it to something better when possible • More stdlib functions ◦ takeIf, takeUnless, with, etc
  54. // Java code public void sendMessageToClient( @Nullable Client client, @Nullable

    String message, @NotNull Mailer mailer ) { if (client == null || message == null) return; PersonalInfo personalInfo = client.getPersonalInfo(); if (personalInfo == null) return; String email = personalInfo.getEmail(); if (email == null) return; mailer.sendMessage(email, message); } stdlib functions in action
  55. // Java code public void sendMessageToClient( @Nullable Client client, @Nullable

    String message, @NotNull Mailer mailer ) { if (client == null || message == null) return; PersonalInfo personalInfo = client.getPersonalInfo(); if (personalInfo == null) return; String email = personalInfo.getEmail(); if (email == null) return; mailer.sendMessage(email, message); } // Kotlin equivalent fun sendMessageToClient( client: Client?, message: String?, mailer: Mailer ) { client?.takeIf { message != null } ?.personalInfo ?.email ?.let { email -> mailer.sendMessage(email, message!!) } } stdlib functions in action
  56. // Java code public void sendMessageToClient( @Nullable Client client, @Nullable

    String message, @NotNull Mailer mailer ) { if (client == null || message == null) return; PersonalInfo personalInfo = client.getPersonalInfo(); if (personalInfo == null) return; String email = personalInfo.getEmail(); if (email == null) return; mailer.sendMessage(email, message); } // Kotlin equivalent fun sendMessageToClient( client: Client?, message: String?, mailer: Mailer ) { client?.takeIf { message != null } ?.personalInfo ?.email ?.let { email -> mailer.sendMessage(email, message!!) } } stdlib functions in action
  57. // Java code public void sendMessageToClient( @Nullable Client client, @Nullable

    String message, @NotNull Mailer mailer ) { if (client == null || message == null) return; PersonalInfo personalInfo = client.getPersonalInfo(); if (personalInfo == null) return; String email = personalInfo.getEmail(); if (email == null) return; mailer.sendMessage(email, message); } // Kotlin equivalent fun sendMessageToClient( client: Client?, message: String?, mailer: Mailer ) { client?.takeIf { message != null } ?.personalInfo ?.email ?.let { email -> mailer.sendMessage(email, message!!) } } stdlib functions in action
  58. // Java code public void sendMessageToClient( @Nullable Client client, @Nullable

    String message, @NotNull Mailer mailer ) { if (client == null || message == null) return; PersonalInfo personalInfo = client.getPersonalInfo(); if (personalInfo == null) return; String email = personalInfo.getEmail(); if (email == null) return; mailer.sendMessage(email, message); } // Kotlin equivalent fun sendMessageToClient( client: Client?, message: String?, mailer: Mailer ) { client?.takeIf { message != null } ?.personalInfo ?.email ?.let { email -> mailer.sendMessage(email, message!!) } } stdlib functions in action Functional, readable, concise
  59. Java & Kotlin Interoperability Important because: • Android APIs are

    written in Java • Kotlin & Java may be mixed in the same codebase
  60. String! String? String Non-nullable type Nullable type Platform type Kotlin

    compiler has no idea whether it’s nullable or not
  61. // Calling Java from Kotlin // Compiles fine. Safe in

    runtime val nameLength = myObject.name.length // Java @NotNull public String getName() { return "Segun"; } ...
  62. // Calling Java from Kotlin // Compiler error. Need safe

    access val nameLength = myObject.name.length // Java @Nullable public String getName() { return null; } ...
  63. // Calling Java from Kotlin // Compiler error. Need safe

    access val nameLength = myObject.name.length // Compiles fine. Safe in runtime val nameLength = myObject.name?.length // Java @Nullable public String getName() { return null; } ...
  64. // Calling Java from Kotlin // Compiles fine. NPE in

    runtime val nameLength = myObject.name.length // Java public String getName() { return null; } ...
  65. // Java public String getName() { return null; } ...

    // Calling Java from Kotlin // Compiles fine. NPE in runtime val nameLength = myObject.name.length // Compiles fine. Safe in runtime val nameLength = myObject.name?.length
  66. @Nullable String getName() {..} Kotlin compiler treats as nullable “?”

    @NotNull String getName() {..} Kotlin compiler treats as non-nullable String getName() {..} Kotlin compiler has no clue. Treats as platform type
  67. Kotlin package-level functions Functions can be defined in a file

    directly without being in any class or object. // InteropDemo.kt fun getName() = "Segun"
  68. Kotlin package-level functions Functions can be defined in a file

    directly without being in any class or object. // InteropDemo.kt fun getName() = "Segun" // Calling from Java InteropDemoKt.getName();
  69. Kotlin package-level functions @JvmName annotation helps to tell Kotlin compiler

    what the generated Java class name should be when consumed from Java
  70. Kotlin package-level functions // InteropDemo.kt @file:JvmName("InteropDemo") fun getName() = "Segun"

    @JvmName annotation helps to tell Kotlin compiler what the generated Java class name should be when consumed from Java
  71. Kotlin package-level functions // InteropDemo.kt @file:JvmName("InteropDemo") fun getName() = "Segun"

    // Calling from Java InteropDemo.getName(); @JvmName annotation helps to tell Kotlin compiler what the generated Java class name should be when consumed from Java
  72. @JvmName // Kotlin code @JvmName("getInteropName") fun getName() = "Segun" //

    Consumed from Java demo.getInteropName(); The annotation can also be applied to functions
  73. @JvmName The annotation can also be applied to functions //

    Kotlin code @JvmName("getInteropName") fun getName() = "Segun" // Consumed from Java demo.getInteropName(); // Consumed from Kotlin demo.getName()
  74. @JvmStatic // Kotlin Object object StringUtils { fun upperCase(text: String)

    = text.toUpperCase() } // Calling from Java String upperCaseName = StringUtils.INSTANCE.upperCase("Segun");
  75. @JvmStatic // Kotlin Object object StringUtils { + @JvmStatic fun

    upperCase(text: String) = text.toUpperCase() } // Calling from Java String upperCaseName = StringUtils.INSTANCE.upperCase("Segun");
  76. @JvmStatic // Kotlin Object object StringUtils { + @JvmStatic fun

    upperCase(text: String) = text.toUpperCase() } // Calling from Java String upperCaseName = StringUtils.INSTANCE.upperCase("Segun"); String upperCaseName = StringUtils.upperCase("Segun");
  77. @Module abstract class RepositoryModule { @Binds abstract fun bindRepo(repository: Repository):

    IRepository companion object { // static provider @Provides fun provideApiService: ApiService = ... } } @JvmStatic - Static providers in Dagger
  78. @Module abstract class RepositoryModule { @Binds abstract fun bindRepo(repository: Repository):

    IRepository companion object { // static provider @Provides fun provideApiService: ApiService = ... } } Won’t compile. Dagger error. Why? @JvmStatic - Static providers in Dagger
  79. @JvmStatic - Static providers in Dagger // Generated Java class

    @Module public abstract class RepositoryModule { ... @Binds @NotNull public abstract IRepository bindRepo(@NotNull Repository var1); public static final class Companion { @Provides @NotNull public final ApiService providesApiService() {...} } }
  80. @JvmStatic - Static providers in Dagger // Generated Java class

    @Module public abstract class RepositoryModule { ... @Binds @NotNull public abstract IRepository bindRepo(@NotNull Repository var1); public static final class Companion { @Provides @NotNull public final ApiService providesApiService() {...} } } No @Module
  81. @JvmStatic - Static providers in Dagger // Generated Java class

    @Module public abstract class RepositoryModule { ... @Binds @NotNull public abstract IRepository bindRepo(@NotNull Repository var1); public static final class Companion { @Provides @NotNull public final ApiService providesApiService() {...} } } No @Module @Provides method is generated as a member of the Companion class
  82. @JvmStatic - Static providers in Dagger // Generated Java class

    @Module public abstract class RepositoryModule { ... @Binds @NotNull public abstract IRepository bindRepo(@NotNull Repository var1); public static final class Companion { @Provides @NotNull public final ApiService providesApiService() {...} } } No @Module @Provides method is generated as a member of the Companion class Dagger is unable to provide this dependency
  83. @JvmStatic - Static providers in Dagger @Module abstract class RepositoryModule

    { @Binds abstract fun bindRepo(repository: Repository): IRepository + @Module companion object { // static provider @Provides + @JvmStatic fun provideApiService: ApiService = ... } } Fixed by adding @Module and @JvmStatic annotations
  84. @JvmStatic - Static providers in Dagger Fixed by adding @Module

    and @JvmStatic annotations // Generated Java class with fixes @Module public abstract class RepositoryModule { @Binds public abstract IRepository bindRepos(@NotNull Repository var1); @Provides @JvmStatic public static final ApiService providesApiService() {...} @Module public static final class Companion { @Provides @JvmStatic public final ApiService providesApiService() {...} ... } } Kotlin compiler generates a static method within the RepositoryModule class
  85. @JvmStatic - Static providers in Dagger Fixed by adding @Module

    and @JvmStatic annotations // Generated Java class with fixes @Module public abstract class RepositoryModule { @Binds public abstract IRepository bindRepos(@NotNull Repository var1); @Provides @JvmStatic public static final ApiService providesApiService() {...} @Module public static final class Companion { @Provides @JvmStatic public final ApiService providesApiService() {...} ... } } Dagger will see this module, but generated code will not be used. RepositoryModule.Companion module isn’t registered
  86. @JvmOverloads // Kotlin class with default values class Point(val x:

    Int = 0, val y: Int = 0) // Calling from Java Point p1 = new Point(); Point p2 = new Point(1, 3);
  87. @JvmOverloads // Kotlin class with default values class Point(val x:

    Int = 0, val y: Int = 0) // Calling from Java Point p1 = new Point(); Point p2 = new Point(1, 3); Specify none or all params
  88. @JvmOverloads // Kotlin class with default values class Point @JvmOverloads

    constructor(val x: Int = 0, val y: Int = 0) // Generated Java code public final class Point { // Overloaded constructors public Point() {...} public Point(int x) {...} public Point(int x, int y) {...} }
  89. @JvmOverloads // Calling from Java Point p1 = new Point();

    Point p2 = new Point(1); Point p3 = new Point(1, 3); // Kotlin class with default values class Point @JvmOverloads constructor(val x: Int = 0, val y: Int = 0) // Generated Java code public final class Point { // Overloaded constructors public Point() {...} public Point(int x) {...} public Point(int x, int y) {...} }