The final parameter or value of the val function or in Kotlin?

Why does Kotlin remove the final or val parameter, which is very useful in Java?

fun say(val msg: String = "Hello World") {
    msg = "Hello To Me" // would give an error here since msg is val    
                        //or final
    ...
    ...
    ...
}
+13
source share
2 answers

The parameters of the Kotlin function are final. There is no keyword val or final, as it is by default (and cannot be changed).

+29
source

After supporting the modified Kotlin M5.1 settings , in earlier versions that can be achieved with

fun foo(var x: Int) {
  x = 5
}

According to Kotlin developers, the main reasons for removing this feature below are

  • , : , , ( ).

  • : "val" "var" , ( , ).

  • , , , "val" "var" infront for-loop.

. val. . -

fun say(val msg: String) {
    var tempMsg = msg
    if(yourConditionSatisfy) {
       tempMsg = "Hello To Me"
    }
}
+2

Source: https://habr.com/ru/post/1660577/


All Articles