Hoje decidi escrever sobre as inovações mais importantes do Kotlin 1.4.0. Acho que os desenvolvedores Android iniciantes ficarão muito interessados neste tópico e poderão aplicar novos recursos em seus aplicativos. Além disso, espero que meu artigo seja útil para desenvolvedores profissionais que realmente amam e ajudam a desenvolver o Kotlin.
Os principais tópicos que abordarei:
Inovações de sintaxe
Novas ferramentas no IDE
Novo compilador
Qualidade e performance
Bem, eu sugiro que você se sirva de um café ou chá, estoque doces e você pode começar)
Kotlin
:
|
|
|
Kotlin 1.4.0 |
17 , 2020, , . , IDE. |
|
Kotlin 1.4.10 Kotlin 1.4.20 Kotlin 1.4.21 |
7 , 2020, Kotlin 1.4.0 23 , 2020, , JVM. 7 , 2020, Kotlin 1.4.20 |
|
SAM -
SAM (SAM - Single Abstract Method, ).
Kotlin , SAM fun
, :
fun interface ItemSelectListener {
fun onItemSelect(position: Int): String
}
val items = listOf("Item 1", "Item 2", "Item 3")
val myListener = ItemSelectListener { position ->
items[position]
}
fun main() {
print("selected item -> ${myListener.onItemSelect(0)}")
}
: RecyclerView .
.
API
Kotlin API .
:
API API
API:
, API
: data , ..
. Kotlin 1.4.0 :
fun foo(a: Int, b: String = "", c: Int) {}
fun main() {
foo(a = 10, "Hello, World", c = 100000)
}
, ( "Hello, World"
). Kotlin .
fun reformat(str: String,
wordSeparator: Char = ' ', //
) {
// TODO
}
, default :
fun foo(a: Int = 0): String = "value -> $a" // 'a' 0
fun apply(f: () -> String): String = f()
fun main() {
println(apply(::foo))
}
, , Unit
.
foo
, , (Unit
). , , :
fun foo(f: () -> Unit) { }
fun returnValue(): Int = 42
fun main() {
foo { returnValue() } // Kotlin 1.4.0
foo(::returnValue) // Kotlin 1.4.0 ,
//
}
, :
fun foo(a: Int, vararg words: String) {}
fun useCase0(f: (Int) -> Unit) {}
fun useCase1(f: (Int, String) -> Unit) {}
fun useCase2(f: (Int, String, String) -> Unit) {}
fun test() {
useCase0(::foo)
useCase1(::foo)
useCase2(::foo)
}
, suspend
fun lockUI() {}
fun takeSuspend(f: suspend () -> Unit) {}
fun test() {
takeSuspend { lockUI() } // Kotlin 1.4.0
takeSuspend(::lockUI) // Kotlin 1.4.0
}
break and continue when , for
Kotlin 1.4.0 break
continue
when
, for
( , )
fun foo(numbers: List<Int>) {
for (num in numbers) {
when {
num % 2 == 0 -> continue
num == 10 -> break
else -> println(x)
}
}
}
IDE
Kotlin :
:
( )
(Gradle, Maven)
/ ,
JVM , framework .
Kotlin ( ).
Kotlin 1.4.0 , .
, Debug Tool Window Intellij IDEA, :
, ( )
, Get Coroutines Dump
:
-
, Kotlin
API
:
. ( Kotlin 1.3 , ). YouTrack
backend ( Kotlin backend, : Kotlin/JVM, Kotlin/JS Kotlin/Native. (IR) Kotlin )
JetBrains frontend .
Frontend - , , , .
IDE, , , Kotlin .
:
60 , IDE
Um aumento na velocidade do IDE, que pode ser visto seguindo o link (aqui é o momento de destacar a sintaxe Kotlin ao abrir um grande projeto). A figura abaixo também mostra o tempo de resposta do preenchimento automático (que diminuiu em comparação com as versões anteriores)
E muitos outros que estão diretamente relacionados à criação de um novo compilador.
Alguns links úteis
Notas de versão do Kotlin 1.4.0 no blog JetBrains
Novos recursos no site oficial do Kotlin
Evento online Kotlin 1.4.0 (em inglês)
Estatísticas do StackOverflow Survey 2020
-