Qyouu
TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊
早期的 Kotlin 版本函數(shù)參數(shù)是可以定義成 var 的,不過(guò)后來(lái)官方明確了「函數(shù)參數(shù)都是不可變」這一點(diǎn)。在這篇更新日志里可以找到說(shuō)明:
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” in front of a
parameter in a function, catch block of for-loop is no longer allowed.
查看完整回答