What's new in Kotlin 1.4.0

Today I decided to write about the most important innovations in Kotlin 1.4.0. I think that novice Android developers will be very interested in this topic and they will be able to apply new features in their applications. Also, I hope that my article will be useful for professional developers who really love and help develop Kotlin.





The main topics that I will cover:





  • Syntax innovations





  • New tools in the IDE





  • New compiler





  • Quality and performance





Well, I suggest you pour yourself some coffee or tea, stock up on sweets and you can start)





A bit about Kotlin releases

:

















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 .





:





  1. API API





  2. API:









    1. , API





  3. : 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 :





:





  1. ( )





  2. (Gradle, Maven)









  3. / ,





  4. JVM , framework .





Kotlin ( ).





Kotlin 1.4.0 , .





, Debug Tool Window Intellij IDEA, :













  1. , ( )





  2. , Get Coroutines Dump





:









  1. , Kotlin





  2. API





:





  1. . ( Kotlin 1.3 , ). YouTrack





  2. backend ( Kotlin backend, : Kotlin/JVM, Kotlin/JS Kotlin/Native. (IR) Kotlin )





JetBrains frontend .





Frontend - , , , .





IDE, , , Kotlin .





:





  1. 60 , IDE





  2. An increase in the speed of the IDE, which can be seen by following the link (here is the time for highlighting Kotlin syntax when opening a large project). The figure below also shows the autocomplete response time (which has decreased compared to previous releases)





  3. And many others that are directly related to the creation of a new compiler.





Some useful links

  1. Kotlin 1.4.0 Release Notes on JetBrains Blog





  2. New features on the official Kotlin website





  3. Kotlin 1.4.0 Online Event (in English)





  4. Statistics from StackOverflow Survey 2020





  5. Statistics from JetBrains












All Articles