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

  • Functional Kotlin
  • Mario Arias Rivu Chakraborty
  • 199字
  • 2021-06-24 19:15:27

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/
主站蜘蛛池模板: 明水县| 额尔古纳市| 凤翔县| 乐平市| 平定县| 巴塘县| 扶绥县| 溧阳市| 陕西省| 商丘市| 石河子市| 慈利县| 明水县| 石城县| 宁晋县| 资中县| 隆尧县| 延吉市| 昭通市| 偏关县| 罗定市| 北流市| 满洲里市| 璧山县| 理塘县| 阳山县| 邵武市| 南溪县| 海丰县| 洞口县| 南漳县| 江油市| 文山县| 大足县| 修水县| 苏尼特右旗| 马鞍山市| 嫩江县| 鲜城| 丁青县| 寿阳县|