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

What's New in Kotlin 1.3

What's New in Kotlin 1.3

Rafael Toledo

October 23, 2018
Tweet

More Decks by Rafael Toledo

Other Decks in Programming

Transcript

  1. O QUE HÁ DE NOVO NO
    KOTLIN 1.3
    Kotlin Meetup SP

    View Slide

  2. /_rafaeltoledo
    /rafaeltoledo

    View Slide

  3. COROUTINES NÃO SÃO MAIS
    EXPERIMENTAIS

    View Slide

  4. KOTLIN NATIVE EM BETA

    View Slide

  5. KOTLIN MULTIPLATAFORMA

    View Slide

  6. CONTRACTS
    fun foo(s: String?) {
    if (s != null) s.length // Compiler automatically casts 's' to 'String'
    }

    View Slide

  7. CONTRACTS
    fun String?.isNotNull(): Boolean = this != null
    fun foo(s: String?) {
    if (s.isNotNull()) s.length // No smartcast :(
    }

    View Slide

  8. CONTRACTS
    fun require(condition: Boolean) {
    // This is a syntax form, which tells compiler:
    // "if this function returns successfully, then passed 'condition' is true"
    contract { returns() implies condition }
    if (!condition) throw IllegalArgumentException(...)
    }
    fun foo(s: String?) {
    require(s is String)
    // s is smartcasted to 'String' here, because otherwise
    // 'require' would have throw an exception
    }

    View Slide

  9. CAPTURANDO VALOR DO
    when EM UMA VARIÁVEL
    fun Request.getBody() =
    when (val response = executeRequest()) {
    is Success -> response.body
    is HttpError -> throw HttpException(response.status)
    }

    View Slide

  10. @JvmStatic e @JvmField
    EM INTERFACES
    interface Foo {
    companion object {
    @JvmField
    val answer: Int = 42
    @JvmStatic
    fun sayHello() {
    println("Hello, world!")
    }
    }
    }

    View Slide

  11. @JvmStatic e @JvmField
    EM INTERFACES
    interface Foo {
    public static int answer = 42;
    public static void sayHello() {
    // ...
    }
    }

    View Slide

  12. DECLARAÇÕES ANINHADAS
    EM CLASSES DE ANOTAÇÃO
    annotation class Foo {
    enum class Direction { UP, DOWN, LEFT, RIGHT }
    annotation class Bar
    companion object {
    fun foo(): Int = 42
    val bar: Int = 42
    }
    }

    View Slide

  13. MAIN SEM PARÂMETROS
    fun main() {
    println("Hello, world!")
    }

    View Slide

  14. ATÉ 255 PARÂMETROS EM
    LAMBDAS
    fun trueEnterpriseComesToKotlin(block: (Any, Any, ... /* 42 more */, Any) -> Any) {
    block(Any(), Any(), ..., Any())
    }

    View Slide

  15. Experimental

    View Slide

  16. INLINE CLASSES
    Inline classes can be viewed as a restricted version of the
    usual classes, in particular, inline classes must have exactly
    one property
    The Kotlin compiler will use this restriction to aggressively
    optimize runtime representation of inline classes and
    substitute their instances with the value of the underlying
    property where possible removing constructor calls, GC
    pressure, and enabling other optimizations

    View Slide

  17. INLINE CLASSES
    inline class Name(val s: String)
    fun main() {
    // In the next line no constructor call happens, and
    // at the runtime 'name' contains just string "Kotlin"
    val name = Name("Kotlin")
    println(name.s)
    }

    View Slide

  18. UNSIGNED INTEGERS
    // You can define unsigned types using literal suffixes
    val uint = 42u
    val ulong = 42uL
    val ubyte: UByte = 255u
    // You can convert signed types to unsigned and vice versa via stdlib extensions:
    val int = uint.toInt()
    val byte = ubyte.toByte()
    val ulong2 = byte.toULong()
    // Unsigned types support similar operators:
    val x = 20u + 22u
    val y = 1u shl 8
    val z = "128".toUByte()
    val range = 1u..5u

    View Slide

  19. @JvmDefault
    interface Foo {
    // Will be generated as 'default' method
    @JvmDefault
    fun foo(): Int = 42
    }

    View Slide

  20. Tooling

    View Slide

  21. kotlinx.serialization
    kotlinx.serialization is a library which provides
    multiplatform support for (de)serializing objects in Kotlin.
    Previously, it was a separate project, but since Kotlin 1.3, it
    ships with the Kotlin compiler distribution on par with the
    other compiler plugins. The main difference is that you don't
    need to manually watch out for the Serialization IDE Plugin
    being compatible with the Kotlin IDE Plugin version you're
    using: now the Kotlin IDE Plugin already includes serialization!

    View Slide

  22. Como começar a experimentar?

    View Slide

  23. Links
    WHAT'S COMING IN KOTLIN 1.3
    https://kotlinlang.org/docs/reference/whatsnew13.html
    A MAJOR RELEASE JUST AROUND THE CORNER - KOTLIN 1.3 M2
    https://blog.jetbrains.com/kotlin/2018/08/kotlin-1-3-m2/
    SEE WHAT'S COMING IN KOTLIN 1.3 M1
    https://blog.jetbrains.com/kotlin/2018/07/see-whats-coming-in-kotlin-1-3-m1/
    JETBRAINS TV - KOTLIN CONF 2018
    https://www.youtube.com/user/JetBrainsTV
    23

    View Slide

  24. OBRIGADO
    Para perguntas e sugestões:
    Rafael Toledo
    [email protected]

    View Slide