K
K
Koshkasobaka2021-04-30 22:30:26
Kotlin
Koshkasobaka, 2021-04-30 22:30:26

Why can't the method increment the variable passed to the arguments?

And I pass a mutable variable var number = 5 to the method

printNumbers(number: Int) {
number++ // подчеркивает красным, предлагая создать локальную переменную
}

It works only if you overwrite in the method:
var number = number
But it looks kind of silly .. or is it supposed to be done this way?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
illuzor, 2021-04-30
@Koshkasobaka

These are the features of kotlin. Function arguments are immutable (we can conditionally assume that they are val).
I do not remember a single case when I would need to change the function argument.

K
Koshkasobaka, 2021-04-30
@Koshkasobaka

So there is only one way out - always overwrite in a local variable?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question