ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:104560] [Ruby master Bug#18035] Introduce general module for immutable by default.
@ 2021-07-09  8:10 samuel
  2021-07-09  8:11 ` [ruby-core:104561] [Ruby master Bug#18035] Introduce general model/semantic " samuel
                   ` (38 more replies)
  0 siblings, 39 replies; 40+ messages in thread
From: samuel @ 2021-07-09  8:10 UTC (permalink / raw)
  To: ruby-core

Issue #18035 has been reported by ioquatix (Samuel Williams).

----------------------------------------
Bug #18035: Introduce general module for immutable by default.
https://bugs.ruby-lang.org/issues/18035

* Author: ioquatix (Samuel Williams)
* Status: Open
* Priority: Normal
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
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 als 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/

^ permalink raw reply	[flat|nested] 40+ messages in thread

end of thread, other threads:[~2024-03-06  3:47 UTC | newest]

Thread overview: 40+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-09  8:10 [ruby-core:104560] [Ruby master Bug#18035] Introduce general module for immutable by default samuel
2021-07-09  8:11 ` [ruby-core:104561] [Ruby master Bug#18035] Introduce general model/semantic " samuel
2021-07-09  8:48 ` [ruby-core:104563] " duerst
2021-07-09  9:51 ` [ruby-core:104566] " samuel
2021-08-10 19:11 ` [ruby-core:104868] [Ruby master Feature#18035] " eregontp
2021-09-22  7:51 ` [ruby-core:105370] " ioquatix (Samuel Williams)
2021-09-22 10:05 ` [ruby-core:105372] " Eregon (Benoit Daloze)
2021-09-22 12:39 ` [ruby-core:105373] " Dan0042 (Daniel DeLorme)
2021-09-22 20:19 ` [ruby-core:105378] " maciej.mensfeld (Maciej Mensfeld)
2021-09-22 20:57 ` [ruby-core:105379] " jeremyevans0 (Jeremy Evans)
2021-09-22 21:25 ` [ruby-core:105381] " tenderlovemaking (Aaron Patterson)
2021-09-22 21:55 ` [ruby-core:105383] " jeremyevans0 (Jeremy Evans)
2021-09-22 22:07 ` [ruby-core:105384] " tenderlovemaking (Aaron Patterson)
2021-09-22 22:10 ` [ruby-core:105385] " ioquatix (Samuel Williams)
2021-09-22 22:32 ` [ruby-core:105386] " jeremyevans0 (Jeremy Evans)
2021-09-22 23:39 ` [ruby-core:105389] " ioquatix (Samuel Williams)
2021-09-23  2:04 ` [ruby-core:105390] " jeremyevans0 (Jeremy Evans)
2021-09-26 23:08 ` [ruby-core:105430] " ioquatix (Samuel Williams)
2021-09-26 23:10 ` [ruby-core:105431] " ioquatix (Samuel Williams)
2021-10-03 12:21 ` [ruby-core:105532] " Eregon (Benoit Daloze)
2021-10-03 12:25 ` [ruby-core:105533] " Eregon (Benoit Daloze)
2021-10-19  4:20 ` [ruby-core:105666] " ko1 (Koichi Sasada)
2021-10-20 14:33 ` [ruby-core:105697] " Eregon (Benoit Daloze)
2021-10-21  3:21 ` [ruby-core:105709] " ioquatix (Samuel Williams)
2021-10-21  9:12 ` [ruby-core:105722] " Eregon (Benoit Daloze)
2021-10-21  9:20 ` [ruby-core:105723] " Eregon (Benoit Daloze)
2021-10-21  9:43 ` [ruby-core:105726] " Eregon (Benoit Daloze)
2021-10-25  1:28 ` [ruby-core:105764] " ioquatix (Samuel Williams)
2021-10-25  1:39 ` [ruby-core:105765] " nobu (Nobuyoshi Nakada)
2021-10-25  2:19 ` [ruby-core:105767] " ioquatix (Samuel Williams)
2021-10-25 14:48 ` [ruby-core:105789] " Dan0042 (Daniel DeLorme)
2021-11-09 16:33 ` [ruby-core:105991] " Dan0042 (Daniel DeLorme)
2021-11-09 16:42 ` [ruby-core:105992] " Eregon (Benoit Daloze)
2021-11-09 16:47 ` [ruby-core:105993] " Eregon (Benoit Daloze)
2023-12-28  8:52 ` [ruby-core:115953] [Ruby master Feature#18035] Introduce general model/semantic for immutability ioquatix (Samuel Williams) via ruby-core
2024-01-04 16:27 ` [ruby-core:116011] " matheusrich (Matheus Richard) via ruby-core
2024-01-05 16:06 ` [ruby-core:116034] " Dan0042 (Daniel DeLorme) via ruby-core
2024-01-14  3:12 ` [ruby-core:116196] " ioquatix (Samuel Williams) via ruby-core
2024-01-14  4:31 ` [ruby-core:116199] " ioquatix (Samuel Williams) via ruby-core
2024-03-06  3:46 ` [ruby-core:117063] " ioquatix (Samuel Williams) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).