官术网_书友最值得收藏!

Implementing immutability in Kotlin

Unlike Clojure, Haskell, F#, and the likes, Kotlin is not a pure functional programming language, where immutability is forced; rather, we may refer to Kotlin as a perfect blend of functional programming and OOP languages. It contains the major benefits of both worlds. So, instead of forcing immutability like pure functional programming languages, Kotlin encourages immutability, giving it automatic preference wherever possible.

In other words, Kotlin has immutable variables (val), but no language mechanisms that would guarantee true deep immutability of the state. If a val variable references a mutable object, its contents can still be modified. We will have a more elaborate discussion and a deeper dive on this topic, but first let us have a look at how we can get referential immutability in Kotlin and the differences between var, val, and const val.

By true deep immutability of the state, we mean a property will always return the same value whenever it is called and that the property never changes its value; we can easily avoid this if we have a  val  property that has a custom getter. You can find more details at the following link:  https://artemzin.com/blog/kotlin-val-does-not-mean-immutable-it-just-means-readonly-yeah/
主站蜘蛛池模板: 肥城市| 灵丘县| 留坝县| 广灵县| 许昌市| 车致| 文昌市| 宽甸| 托克逊县| 龙口市| 仁布县| 互助| 哈尔滨市| 齐河县| 大名县| 保靖县| 县级市| 磴口县| 荥经县| 宣化县| 巴彦县| 常宁市| 天全县| 江油市| 榕江县| 阳山县| 安康市| 乐业县| 忻州市| 太谷县| 东方市| 德昌县| 石阡县| 临沂市| 寿宁县| 肇庆市| 鹰潭市| 龙江县| 吉木乃县| 柳河县| 绥阳县|