A grasp of immutable datastructures

前端 未结 3 981
执笔经年
执笔经年 2020-12-05 03:28

I am learning scala and as a good student I try to obey all rules I found.

One rule is: IMMUTABILITY!!!

So I have tried to code everything with immutable dat

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 03:41

    The rule is not really immutability, but referential transparency. It's perfectly OK to use locally declared mutable variables and arrays, because none of the effects are observable to any other parts of the overall program.

    The principle of referential transparency (RT) is this:

    An expression e is referentially transparent if for all programs p every occurrence of e in p can be replaced with the result of evaluating e, without affecting the observable result of p.

    Note that if e creates and mutates some local state, it doesn't violate RT since nobody can observe this happening.

    That said, I very much doubt that your implementation is any more straightforward with vars.

提交回复
热议问题