kotlin - The `val` in the for range is not immutable? -


in documentation, val assign-once (read-only) local variable. however, below loop assigns values x several times. different concept c++ const?

fun main(args: array<string>) {     val x = 6     val y = 9     (x in 1..y) {         println("fits in range $x")     } } 

output:

fits in range 1 fits in range 2 fits in range 3 fits in range 4 fits in range 5 fits in range 6 fits in range 7 fits in range 8 fits in range 9 

the x used in range construct shadowing val x = 6 - 2 different variables in 2 different scopes.

you can see writing:

val x = 6 val y = 9 (x in 1..y) {     println("fits in range $x") }  println(x) 

the last call print original value - 6


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -