Hoy decidí escribir sobre las innovaciones más importantes en Kotlin 1.4.0. Creo que los desarrolladores de Android novatos estarán muy interesados en este tema y podrán aplicar nuevas funciones en sus aplicaciones. Además, espero que mi artículo sea útil para los desarrolladores profesionales que realmente aman y ayudan a desarrollar Kotlin.
Los principales temas que cubriré:
Innovaciones de sintaxis
Nuevas herramientas en el IDE
Nuevo compilador
Calidad y desempeño
Bueno, le sugiero que se sirva un poco de café o té, se abastezca de dulces y puede comenzar)
Un poco sobre los lanzamientos de 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
Un aumento en la velocidad del IDE, que se puede ver siguiendo el enlace (aquí es el momento de resaltar la sintaxis de Kotlin al abrir un proyecto grande). La figura siguiente también muestra el tiempo de respuesta de autocompletar (que ha disminuido en comparación con versiones anteriores)
Y muchos otros que están directamente relacionados con la creación de un nuevo compilador.
Algunos enlaces útiles
Notas de la versión de Kotlin 1.4.0 en el blog de JetBrains
Nuevas funciones en el sitio web oficial de Kotlin
Evento en línea de Kotlin 1.4.0 (en inglés)
Estadísticas de StackOverflow Survey 2020
-