Share
Embed
Kotlin 1.1: o que vem por aí?
Apresentada no Kotlin Night São Paulo 2017
Rafael Toledo
February 22, 2017
Transcript
33 slides
-
Kotlin 1.1 M01 - 14 julho 2016 M02 - 20
outubro 2016 M03 - 24 novembro 2016 M04 - 21 dezembro 2016 @_rafaeltoledo Beta - 19 janeiro 2017 Beta 2 - 2 fevereiro 2017 RC - 17 fevereiro 2017 -
Type Alias typealias Length = Double typealias Weight = Double
typealias Action<T> = (T) -> Unit typealias Multimap<K, V> = Map<K, List<V>> @_rafaeltoledo -
Herança em Data Classes open class Base(val baseParam: Int) data
class Derived(val param: String): Base(param.lenght) @_rafaeltoledo -
Sealed Classes sealed class Sealed class Derived : Sealed() {
//... } // Em outro arquivo class Other : Sealed() { //... } @_rafaeltoledo -
Method Reference val numberRegex = "\\d+".toRegex() val numbers = listOf("abc",
"123", "456").filter { numberRegex.matches(it) } @_rafaeltoledo -
Method Reference val numberRegex = "\\d+".toRegex() val numbers = listOf("abc",
"123", "456").filter( numberRegex::matches) @_rafaeltoledo -
Underscores para parâmetros fun loadProduct(id: Int) { api.fetchProduct(id) { (_,
name, date) -> title.text = name createdOn.text = date.format() } } @_rafaeltoledo -
Desestrutura de classes data class User(val username: String, val email:
String) fun printUserData() { val (username, email) = fetchUser() println("${username}: ${email}") } @_rafaeltoledo -
Underscores para numerais val oneMillion = 1_000_000 val hexBytes =
0xFF_EC_DE_5E val bytes = 0b11010010_01101001_10010100_10010010 @_rafaeltoledo -
Enums utilizados como generics enum class RGB { RED, GREEN,
BLUE } print(enumValues<RGB>().joinToString { it.name }) @_rafaeltoledo -
mod agora é rem // Operador mod marcado como deprecated
val deprecatedValue = 247.mod(2) val value = 247.rem(2) @_rafaeltoledo -
Coroutines - experimental É basicamente uma thread “leve” que pode
ser pausada e retomada posteriormente “We think that now we have got a great design of coroutines for Kotlin, but we realize that it has not been battle-tested enough.” @_rafaeltoledo -
Coroutines fun someWork(): CompletableFuture<String> = ... fun moreWork() = async
{ println("Work started") val str = await(someWork()) println("Work completed") } @_rafaeltoledo -
Coroutines fun someWork(): CompletableFuture<String> = ... // CompletableFuture = minSdkVersion
24 fun moreWork() = async { println("Work started") val str = await(someWork()) println("Work completed") } @_rafaeltoledo -
Coroutines fun someWork(): CompletableFuture<String> = ... // CompletableFuture = minSdkVersion
24 fun moreWork() = async { println("Work started") val str = await(someWork()) println("Work completed") } @_rafaeltoledo -
Coroutines github.com/kotlin/kotlinx.coroutines Documentação Extensões e utilitários JDK8, NIO (JDK 7),
Swing, JavaFX e RxJava @_rafaeltoledo -
JavaScript Não é mais considerado experimental :) Standard Library unificada.
(ex. collections) Melhor geração de código - amigável a minifiers, linters, otimizadores, etc. Modificador external - para acesso a classes implementadas em JS @_rafaeltoledo -
Conversões String / Números String.toIntOrNull(): Int? String.toDoubleOrNull(): Double? val port
= System.getenv("PORT") ?.toIntOrNull() ?: 80 @_rafaeltoledo -
Extensões val outDirFile = File(outputDir.path).takeIf { it.exists() } ?: return
false val charFrequencies: Map<Char, Int> = text.groupingBy { it }.eachCount() @_rafaeltoledo -
minOf / maxOf val list1 = listOf("a", "b") val list2
= listOf("x", "y", "z") val minSize = minOf(list1.size, list2.size) val longestList = maxOf(list1, list2, compareBy { it.size }) @_rafaeltoledo -
Map com valores default val map = mapOf("key" to 42)
// returns non-nullable Int value 42 val value: Int = map.getValue("key") // throws NoSuchElementException map.getValue("key2") val mapWithDefault = map.withDefault { k -> k.length } // returns 4 val value2 = mapWithDefault.getValue("key2") @_rafaeltoledo -
Subtração com Maps val map = mapOf("key" to 42) val
emptyMap = map - "key" @_rafaeltoledo -
Gradle buildscript { ext.kotlinVersion = '1.1.0-rc-91' repositories { jcenter() maven
{ url 'http://dl.bintray.com/kotlin/kotlin-eap-1.1' } } dependencies { ... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" ... } } @_rafaeltoledo -
Referências & Links Blog Oficial blog.jetbrains.com/kotlin What’s New 1.1 kotlinlang.org/docs/reference/whatsnew11.html
Kotlin Evolution & Enhancement Process github.com/kotlin/KEEP Bug Tracker youtrack.jetbrains.com/issues/KT -
www.concretesolutions.com.br Rio de Janeiro – Rua São José, 90 –
cj. 2121 Centro – (21) 2240-2030 São Paulo - Av. Nações Unidas, 11.541 3º andar - Brooklin - (11) 4119-0449 Ajudamos empresas a criar produtos digitais de sucesso