From: "Eregon (Benoit Daloze)" Date: 2021-10-20T14:33:56+00:00 Subject: [ruby-core:105697] [Ruby master Feature#18035] Introduce general model/semantic for immutable by default. Issue #18035 has been updated by Eregon (Benoit Daloze). Some notes from the discussion on Slack: In general shareable (from Ractor.make_shareable) != immutable, notably for special shareable objects like Ractor, and potentially Ractor::Array (which would share any element) or so. So the deep_frozen/immutable flag should be something different. For the special case of Module/Class I believe nobody wants them frozen for obj.deep_freeze or so, so that's a case of shareable and "deep freezing/immutable" agree, but not for other special-shareable objects. Immutable does imply shareable, but not the other way around. Re half-frozen if some override of `freeze` raises an exception, that's fine, the bug should be fixed in the bad freeze, it was already discussed extensively when adding Ractor.make_shareable. The exception will prevent `freeze/deep_freeze/Immutable#new` to return which is good enough to indicate the problem. Implementation-wise, I guess we can reuse quite a bit from `Ractor.make_shareable`, but it needs to be a different flag, and it needs to freeze more than `Ractor.make_shareable` (absolutely everything except Module/Class). ---------------------------------------- Feature #18035: Introduce general model/semantic for immutable by default. https://bugs.ruby-lang.org/issues/18035#change-94196 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- It would be good to establish some rules around mutability, immutability, frozen, and deep frozen in Ruby. I see time and time again, incorrect assumptions about how this works in production code. Constants that aren't really constant, people using `#freeze` incorrectly, etc. I don't have any particular preference but: - We should establish consistent patterns where possible, e.g. - Objects created by `new` are mutable. - Objects created by literal are immutable. We have problems with how `freeze` works on composite data types, e.g. `Hash#freeze` does not impact children keys/values, same for Array. Do we need to introduce `freeze(true)` or `#deep_freeze` or some other method? Because of this, frozen does not necessarily correspond to immutable. This is an issue which causes real world problems. I also propose to codify this where possible, in terms of "this class of object is immutable" should be enforced by the language/runtime, e.g. ```ruby module Immutable def new(...) super.freeze end end class MyImmutableObject extend Immutable def initialize(x) @x = x end def freeze return self if frozen? @x.freeze super end end o = MyImmutableObject.new([1, 2, 3]) puts o.frozen? ``` Finally, this area has an impact to thread and fiber safe programming, so it is becoming more relevant and I believe that the current approach which is rather adhoc is insufficient. I know that it's non-trivial to retrofit existing code, but maybe it can be done via magic comment, etc, which we already did for frozen string literals. -- https://bugs.ruby-lang.org/ Unsubscribe: