I'm trying to reuse function parameters some thing like this
fun test(i: Int) {
i += 5
}
but as noted in this distinction
Function parameters are
val
notvar
I'm trying to reuse function parameters some thing like this
fun test(i: Int) {
i += 5
}
but as noted in this distinction
Function parameters are
val
notvar
In Kotlin, function arguments are treated as val
. That means you'll have to do something inside your function in order to "modifty" its reference.
Your solution will work, but I feel that it's a bad practice to shadow variables. It leads to confusion, and doesn't quite accurately cover the intent that you understand that the effect is local to the function.
I would go with something like this:
fun test(i: Int) {
var i2 = i
i2 += 3 // etc...
}
the only solution I found is to use name shadowing i.e something like that
fun test(i: Int) {
var i = i
i += 5
}
I'm not sure if it the best solution because it doesn't feel right, even IntelliJ IDEA warns me about it.
I was hoping for thing magical like
fun test(var i: Int) {
i += 5
}
but unfortuantly this doesn't even comile.