Skip to content

Commit e42dcd8

Browse files
authored
Merge pull request #58 from dry-rb/deprecate-blocks-in-memoizable
Add warning about memoization block-accepting methods
2 parents 8f2a47b + 10854fc commit e42dcd8

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

lib/dry/core/memoizable.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "dry/core/deprecations"
4+
35
module Dry
46
module Core
57
module Memoizable
@@ -80,6 +82,15 @@ def #{method.name}
8082
mapping = parameters.to_h { |k, v = nil| [k, v] }
8183
params, binds = declaration(parameters, mapping)
8284

85+
if parameters.last[0].eql?(:block)
86+
Deprecations.warn(<<~WARN)
87+
Memoization for block-accepting methods isn't safe.
88+
Every call creates a new block instance bloating cached results.
89+
In the future, blocks will still be allowed but won't participate in
90+
cache key calculation.
91+
WARN
92+
end
93+
8394
module_eval <<~RUBY, __FILE__, __LINE__ + 1
8495
def #{method.name}(#{params.join(", ")})
8596
key = [:"#{method.name}", #{binds.join(", ")}].hash

0 commit comments

Comments
 (0)