How to make Scala's immutable collections hold immutable objects

大兔子大兔子 提交于 2019-12-05 06:01:05

This is not possible out-of-the-box with scala via some specific language construct unless you have followed the idiom that all of your objects are immutable, in which case this behaviour comes for free!

With 2.8, named parameters have made "copy constructors" quite nice to use, from a readability perspective. But you are correct, this works as copy-on-write. The behaviour you are asking for, where the "current" object is the only one mutated goes completely against the way the JVM works, unfortunately (for you)!

Actually the phrase "the current object" makes no sense; really you mean "the current reference"! All other references (outside the current lexical scope) which point to the same object, erm, point to the same object! There is only one object!

Hence it's just not possible for this object to appear to be mutable from the perspective of the current lexical scope but immutable for others

Matt

If you're interested in some more general theory on how to handle updates to immutable data structures efficiently,

http://en.wikipedia.org/wiki/Zipper_%28data_structure%29

might prove interesting.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!