Kotlin Function Parameters As val


#1

Hi. I am seeing different behavior in the REPL vs compilation for following code sample. Based on this blog post and this I can assume compilation is correctly erroring out so this is a bug in the REPL? That I can change the value of the function parameter?

Also with regards to the change of function parameters to being val only, I am wondering if the grammar as specified here (which I assume is generated from here) is still correct?

$: kotlinc -version
info: Kotlin Compiler version 1.0.1-2
[Mon May 30 00:35:55 ashwin.amit@Ashwin-Amit:/tmp ]
$: cat -n a.kt 
 1  package com.company
 2
 3
 4  fun test(n: Int) {
 5      n = 1
 6      println(n)
 7  }
[Mon May 30 00:35:58 ashwin.amit@Ashwin-Amit:/tmp ]
$: kotlinc a.kt
a.kt:5:5: error: val cannot be reassigned
n = 1
^
[Mon May 30 00:36:03 ashwin.amit@Ashwin-Amit:/tmp ]
$: kotlinc
Welcome to Kotlin version 1.0.1-2 (JRE 1.8.0_77-b03)
Type :help for help, :quit for quit
>>> fun test(n: Int) {
...   n = 5
...   println(n)
... }
>>> test(1)
5

#2

Yes, this is a bug in the REPL. I’ve filed https://youtrack.jetbrains.com/issue/KT-12544

The grammar needs to be updated too.