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

Let Kotlin Come To You

Let Kotlin Come To You

Kotlin is a statically typed, multi-platform language, developed by JetBrains. It’s use has increased dramatically in the past year. What does the language offer us, and why might we choose it? In this talk we’ll go over some of the basics of Kotlin, the benefits, and how we can use it in our day to day development. You will walk away from this talk with the tools you need to get started learning and building with Kotlin, whether you’re working with the JVM, JavaScript, or native.

Victoria Gonda

April 25, 2018
Tweet

More Decks by Victoria Gonda

Other Decks in Programming

Transcript

  1. LET KOTLIN COME TO YOU
    VICTORIA GONDA
    VICTORIAGONDA.COM

    View Slide

  2. @TTGonda
    WHO AM I?
    ▸ Android Engineer at Buffer
    ▸ Author at Ray Wenderlich
    ▸ Chicago
    ▸ Dancer

    View Slide

  3. @TTGonda

    View Slide

  4. “STATICALLY TYPED PROGRAMMING
    LANGUAGE FOR MODERN
    MULTIPLATFORM APPLICATIONS”
    kotlinlang.org

    View Slide

  5. @TTGonda
    KOTLIN
    ▸ Developed by JetBrains
    ▸ Available for JVM, Android, JavaScript, and Native
    ▸ Concise, safe interoperable, tool-friendly
    ▸ Built in IDE support (with JetBrains products)

    View Slide

  6. @TTGonda
    KOTLIN
    ▸ Strong, static, inferred, null safe typing
    ▸ Immutability by default
    ▸ Smart casting
    ▸ Higher order functions
    ▸ Boilerplate reduction

    View Slide

  7. TYPING

    View Slide

  8. @TTGonda
    STATICALLY TYPED
    final String name = "Victoria";
    val name = "Victoria"

    View Slide

  9. @TTGonda
    NULL TYPING
    val icecream: IceCream?

    View Slide

  10. @TTGonda
    icecream.flavor

    View Slide

  11. @TTGonda
    if (icecream != null) {
    return icecream.flavor
    } else {
    return null
    }

    View Slide

  12. @TTGonda
    SAFE CALL OPERATOR
    return icecream?.flavor

    View Slide

  13. @TTGonda
    !! OPERATOR
    return icecream!!.flavor

    View Slide

  14. @TTGonda
    ELVIS OPERATOR
    return icecream?.flavor ?: "Chocolate Chip"

    View Slide

  15. @TTGonda
    NULL SAFE SCOPING WITH HIGHER ORDER FUNCTIONS
    return icecream?.let({ nonNullIceCream ->

    nonNullIceCream.flavor })

    return icecream?.let({ it.flavor })

    return icecream?.let { it.flavor }

    return icecream?.let(IceCream::flavor)

    View Slide

  16. IMMUTABILITY

    View Slide

  17. @TTGonda
    val conference = "GOTO Chicago"
    conference = "GOTO Chicago 2018”

    View Slide

  18. @TTGonda
    var conference = "GOTO Chicago"
    conference = "GOTO Chicago 2018”

    View Slide

  19. @TTGonda
    val readOnlylist = listOf(1, 2, 3)
    val mutableList = mutableListOf("a", "b", “c")

    View Slide

  20. SMART CASTING

    View Slide

  21. @TTGonda
    SMART CASTING
    class Conference(val year: String): Event()
    fun smartCasting(event: Event) {
    if (event is Conference) {
    event.year
    }
    }

    View Slide

  22. BOILERPLATE
    REDUCTION

    View Slide

  23. @TTGonda
    class Cupcake(
    val flavor: String,
    var icing: String?
    )
    CLASSES

    View Slide

  24. @TTGonda
    public final class Cupcake {
    @NotNull
    private final String flavor;
    @Nullable
    private String icing;
    @NotNull
    public final String getFlavor() {}
    @Nullable
    public final String getIcing() {}
    public final void setIcing(@Nullable String var1) {}
    public Cupcake(@NotNull String flavor, @Nullable String icing) {}
    }

    View Slide

  25. @TTGonda
    data class Cupcake(
    val flavor: String,
    var icing: String?
    )

    View Slide

  26. @TTGonda
    public final class Cupcake {
    // …
    @NotNull
    public final String component1() {}
    @Nullable
    public final String component2() {}
    @NotNull
    public final Cupcake copy(@NotNull String flavor, @Nullable String icing) {}
    public String toString() {}
    public int hashCode() {}
    public boolean equals(Object var1) {}
    }

    View Slide

  27. SEALED CLASSES

    View Slide

  28. @TTGonda
    ENUMS
    enum class GetConferenceState {
    SUCCESS, ERROR, PROGRESS
    }

    View Slide

  29. @TTGonda
    SEALED CLASSES
    sealed class GetConferenceState {
    class Success(val result: Conference): GetConferenceState()
    data class Error(val errorMessage: String): GetConferenceState()
    object InProgress: GetConferenceState()
    }

    View Slide

  30. @TTGonda
    SEALED CLASSES
    when(state) {
    is GetConferenceState.Success -> showConference(state.conference)
    is GetConferenceState.Error -> showError(state.errorMessage)
    is GetConferenceState.InProgress -> showLoading()
    }

    View Slide

  31. @TTGonda
    AND MORE!

    View Slide

  32. @TTGonda

    View Slide

  33. WHERE CAN KOTLIN BE
    USED?

    View Slide

  34. @TTGonda
    WHERE CAN KOTLIN BE USED?
    ▸ JVM, Android, JavaScript, Native
    ▸ Mobile, Server, Browser, Views
    ▸ IntelliJ, Android Studio, Eclipse, Standalone Compilers
    ▸ Kotlin all the things!
    ▸ Try it out in the browser (try.kotlinlang.org)

    View Slide

  35. @TTGonda
    JAVA VIRTUAL MACHINE
    ▸ Use on the server, Android, Java applications
    ▸ Spring Boot, for example
    ▸ Anywhere the JVM lives
    ▸ Interoperable with Java
    ▸ More flexible and concise than Java
    ▸ Less feature bloat than Scala

    View Slide

  36. @TTGonda
    JVM - INTELLIJ

    View Slide

  37. @TTGonda
    JVM - INTELLIJ
    ▸ New Kotlin file in src directory
    fun main(args: Array) {
    println("Hello GOTO Chicago")
    }
    ▸ Run! ▶

    View Slide

  38. @TTGonda
    JVM - GRADLE
    ▸ Set up gradle-kotlin plugin and apply it. Add dependencies.
    repositories {
    mavenCentral()
    }
    dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib"
    }
    ▸ Alternatively in IntelliJ, Tools > Kotlin > Configure Kotlin in Project

    View Slide

  39. @TTGonda
    JVM - ECLIPSE
    ▸ Download plugin from Marketplace
    ▸ File > New > Kotlin Project

    View Slide

  40. @TTGonda
    JVM - ECLIPSE
    ▸ New Kotlin file in src directory
    fun main(args: Array) {
    println("Hello GOTO Chicago")
    }
    ▸ Run As > Kotlin Application ▶

    View Slide

  41. @TTGonda
    JVM - COMMAND LINE
    ▸ Install Kotlin
    // hello.kt
    fun main(args: Array) {
    println("Hello GOTO Chicago")
    }
    ▸ Run! ▶
    $ kotlinc hello.kt -include-runtime -d hello.jar
    $ java -jar hello.jar

    View Slide

  42. @TTGonda
    JAVA EQUIVALENT
    public final class HelloKt {
    public static final void main(@NotNull String[] args) {
    Intrinsics.checkParameterIsNotNull(args, "args");
    String var1 = "Hello GOTO Chicago";
    System.out.println(var1);
    }
    }

    View Slide

  43. @TTGonda
    CONVERTING JAVA TO KOTLIN
    ▸ Android Studio and IntelliJ have built in conversion
    ▸ Three ways to get there
    ▸ Code > Convert Java File to Kotlin File
    ▸ ⎇⇧⌘K
    ▸ Search for action “Convert File to Kotlin File

    View Slide

  44. @TTGonda
    JVM - OTHER OPTIONS
    ▸ Ant
    ▸ Maven
    ▸ Griffon

    View Slide

  45. @TTGonda
    ANDROID
    ▸ More flexible and concise than Java
    ▸ Interoperable with Java
    ▸ Can use all the same libraries
    ▸ Supported in Android Studio

    View Slide

  46. @TTGonda
    ANDROID STUDIO

    View Slide

  47. @TTGonda
    JAVASCRIPT
    ▸ Use in browser, and any JS framework
    ▸ ReactJS, for example
    ▸ Type safe
    ▸ Interop with JavaScript

    View Slide

  48. @TTGonda
    JS - INTELLIJ

    View Slide

  49. @TTGonda
    JS - INTELLIJ
    ▸ New Kotlin file in src directory
    fun main(args: Array) {
    println("Hello GOTO Chicago")
    }

    View Slide

  50. @TTGonda
    JS - INTELLIJ
    ▸ Create index.html that runs script
    <br/>
    <br/>
    ▸ Build > Build Application
    ▸ Open index.html in browser ▶

    View Slide

  51. @TTGonda
    JS - COMMAND LINE
    ▸ Library code is in library.kt
    kotlinc-js -output sample-library.js -meta-info library.kt
    ▸ Output:
    sample-library.js
    sample-library.meta.js

    View Slide

  52. @TTGonda
    JS - OTHER OPTIONS
    ▸ Gradle
    ▸ Maven

    View Slide

  53. @TTGonda
    VIEWS
    ▸ Use kotlinx.html library to interact with, and build HTML views
    ▸ Use Anko to build views for Android
    ▸ You can use Kotlin for the logic of what to display in your views!

    View Slide

  54. @TTGonda
    VIEWS
    ▸ kotlinx.html
    val myDiv = document.create.div {
    p { +"text inside" }
    }

    View Slide

  55. @TTGonda
    NATIVE
    ▸ Available for macOS, Linux and Windows
    ▸ LLVM
    ▸ Interop with C libraries

    View Slide

  56. @TTGonda
    NATIVE - COMMAND LINE
    ▸ Download compiler for your OS
    ▸ Create file hello.kt
    fun main(args: Array) {
    println("Hello GOTO Chicago")
    }
    ▸ Compile

    konanc hello.kt
    ▸ Run ▶

    /hello.kexe

    View Slide

  57. @TTGonda
    NATIVE - OTHER OPTIONS
    ▸ CLion IDE
    ▸ Gradle

    View Slide

  58. MULTI PLATFORM

    View Slide

  59. @TTGonda
    MULTI PLATFORM
    ▸ Share logic across platforms
    ▸ Write once, use everywhere
    ▸ Keep native for any framework components
    ▸ Note: experimental

    View Slide

  60. @TTGonda
    MULTI PLATFORM
    ▸ Common module with code shared across
    platforms
    ▸ Platform specific module
    ▸ “Interface” for the common code to call
    platform specific implementations
    http://developine.com/kotlin-native-ios-development-multiplatform-project/

    View Slide

  61. @TTGonda
    MULTI PLATFORM - INTELLIJ

    View Slide

  62. @TTGonda
    common

    Kotlin
    JVM

    Kotlin
    JS

    Kotlin
    *.class *.js

    View Slide

  63. @TTGonda
    // In common module
    expect class Dog(bar: String) {
    fun bark()
    }
    fun main(args: Array) {
    Dog(“Spot").bark()
    }
    // In platform module
    actual class Dog actual constructor(val name: String) {
    actual fun bark() {
    println(“$name says ruff”)
    }
    }

    View Slide

  64. @TTGonda
    GET STARTED!
    ▸ kotlinlang.org docs
    ▸ try.kotlinlang.org to try Kotlin in the browser
    ▸ kotlin.link to find anything Kotlin you might want
    ▸ github.com/vgonda/Talk-Resources my resources from this talk (and others)
    ▸ Kotlin Slack
    ▸ Find me online!

    View Slide

  65. THANKS!
    VICTORIA GONDA
    VICTORIAGONDA.COM

    View Slide