Var in parameters definition

Suggested design. No need to change memory design for the feature as code generation for current design can be utilized.
fun simple(var age:Int):Int{
age = 12
// some extra code might be here
return age
}

This is what’s supported
fun simple(age:Int):Int{
var age1 = age
// make changes to age code here
return age
}

1 Like

AFAIK it was done on purpose by Kotlin designers. Maybe you could present a full example where exactly in your opinion it would make a code better.

2 Likes

I believe this has been discussed extensively elsewhere. If someone can link it that would be better than rehashing it here.

1 Like

I’ll appreciate the link

We removed support for mutable parameters, as in

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

The main reason is that this was confusing: people tend to think that this means passing a parameter by reference, which we do not support (it is costly at runtime). Another source of confusion is primary constructors: “val” or “var” in a constructor declaration means something different from the same thing if a function declarations (namely, it creates a property). Also, we all know that mutating parameters is no good style, so writing “val” or “var” infront of a parameter in a function, catch block of for-loop is no longer allowed.

discussion:

9 Likes

This is what’s also supported:

fun simple(age:Int):Int{
var age = age
// make changes to age code here
return age
}

This leads to a warning in IDEA, but if your coding style allows it, just disable the corresponding inspection.

Instead of mutating parameters (which would be confusing), Kotlin may extend its support for name shadowing, as was discussed here: