Project

General

Profile

Feature #17145

Ractor-aware `Object#deep_freeze`

Added by marcandre (Marc-Andre Lafortune) 6 months ago. Updated 4 months ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:99885]

Description

I'd like to propose Object#deep_freeze:

Freezes recursively the contents of the receiver (by calling deep_freeze) and
then the receiver itself (by calling freeze).
Values that are shareable via Ractor (e.g. classes) are never frozen this way.

# freezes recursively:
ast = [:hash, [:pair, [:str, 'hello'], [:sym, :world]]].deep_freeze
ast.dig(1, 1) # => [:str, 'hello']
ast.dig(1, 1).compact! # => FrozenError

# does not freeze classes:
[[String]].deep_freeze
String.frozen? # => false

# calls `freeze`:
class Foo
  def freeze
    build_cache!
    puts "Ready for freeze"
    super
  end
  # ...
end
[[[Foo.new]]].deep_freeze # => Outputs "Ready for freeze"

I think a variant deep_freeze! that raises an exception if the result isn't Ractor-shareable would be useful too:

class Fire
  def freeze
    # do not call super
  end
end

x = [Fire.new]
x.deep_freeze! # => "Could not be deeply-frozen: #<Fire:0x00007ff151994748>"

Related issues

Related to Ruby master - Feature #2509: Recursive freezing?Rejectedmatz (Yukihiro Matsumoto)12/21/2009Actions
Related to Ruby master - Feature #17100: Ractor: a proposal for a new concurrent abstraction without thread-safety issuesClosedko1 (Koichi Sasada)Actions
Related to Ruby master - Feature #17274: Ractor.make_shareable(obj)ClosedActions
Related to Ruby master - Feature #17273: shareable_constant_value pragmaClosedActions

Also available in: Atom PDF