Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing memoized methods using external arg names. #1817

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions spec/lucky/memoize_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ private class ObjectWithMemoizedMethods
@times_method_5_called += 1
"Boom!"
end

memoize def method_6(for string : String) : String
string.upcase
end
end

describe "memoizations" do
Expand Down Expand Up @@ -125,4 +129,11 @@ describe "memoizations" do
object.method_5__uncached!.should eq("Boom!")
object.times_method_5_called.should eq(3)
end

it "allows for external arg names" do
object = ObjectWithMemoizedMethods.new

object.method_6("boom").should eq("BOOM")
object.method_6(for: "memoize").should eq("MEMOIZE")
end
end
26 changes: 19 additions & 7 deletions src/lucky/memoizable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ module Lucky::Memoizable
# Returns uncached value
def {{ safe_method_name }}__uncached{% if special_ending %}{{ special_ending.id }}{% end %}(
{% for arg in method_def.args %}
{{ arg.name }} : {{ arg.restriction }},
{% if arg.name == arg.internal_name %}
{{ arg.name }} : {{ arg.restriction }},
{% else %}
{{ arg.name }} {{ arg.internal_name }} : {{ arg.restriction }},
{% end %}
{% end %}
) : {{ method_def.return_type }}
{{ method_def.body }}
Expand All @@ -61,7 +65,11 @@ module Lucky::Memoizable
# or the arguments do not match
def {{ safe_method_name }}__tuple_cached{% if special_ending %}{{ special_ending.id }}{% end %}(
{% for arg in method_def.args %}
{{ arg.name }} : {{ arg.restriction }},
{% if arg.name == arg.internal_name %}
{{ arg.name }} : {{ arg.restriction }},
{% else %}
{{ arg.name }} {{ arg.internal_name }} : {{ arg.restriction }},
{% end %}
{% end %}
) : Tuple(
{{ method_def.return_type }},
Expand All @@ -70,18 +78,18 @@ module Lucky::Memoizable
{% end %}
)
{% for arg, index in method_def.args %}
@__memoized_{{ safe_method_name }} = nil if {{arg.name}} != @__memoized_{{ safe_method_name }}.try &.at({{index}} + 1)
@__memoized_{{ safe_method_name }} = nil if {{arg.internal_name}} != @__memoized_{{ safe_method_name }}.try &.at({{index}} + 1)
{% end %}
@__memoized_{{ safe_method_name }} ||= -> do
result = {{ safe_method_name }}__uncached{% if special_ending %}{{ special_ending.id }}{% end %}(
{% for arg in method_def.args %}
{{arg.name}},
{{arg.internal_name}},
{% end %}
)
{
result,
{% for arg in method_def.args %}
{{arg.name}},
{{arg.internal_name}},
{% end %}
}
end.call.not_nil!
Expand All @@ -91,12 +99,16 @@ module Lucky::Memoizable
def {{ method_def.name }}(
{% for arg in method_def.args %}
{% has_default = arg.default_value || arg.default_value == false || arg.default_value == nil %}
{{ arg.name }} : {{ arg.restriction }}{% if has_default %} = {{ arg.default_value }}{% end %},
{% if arg.name == arg.internal_name %}
{{ arg.name }} : {{ arg.restriction }}{% if has_default %} = {{ arg.default_value }}{% end %},
{% else %}
{{ arg.name }} {{ arg.internal_name }} : {{ arg.restriction }}{% if has_default %} = {{ arg.default_value }}{% end %},
{% end %}
{% end %}
) : {{ method_def.return_type }}
{{ safe_method_name }}__tuple_cached{% if special_ending %}{{ special_ending.id }}{% end %}(
{% for arg in method_def.args %}
{{arg.name}},
{{arg.internal_name}},
{% end %}
).first
end
Expand Down
Loading