Skip to content

Commit

Permalink
Use safe var name in getter/setter/property macro
Browse files Browse the repository at this point in the history
Previously, using a local variable called `value` or calling a method
with that name without an explicit `self` receiver would collide with
the one defined in the `getter`, `setter`, and `property` blocks. This
commit uses a safe variable name to avoid that collision.
  • Loading branch information
jgaskins committed Nov 30, 2024
1 parent b87d3e8 commit 761e253
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/object.cr
Original file line number Diff line number Diff line change
Expand Up @@ -457,18 +457,18 @@ class Object
{{var_prefix}}\{{name.var.id}} : \{{name.type}}?

def {{method_prefix}}\{{name.var.id}} : \{{name.type}}
if (value = {{var_prefix}}\{{name.var.id}}).nil?
if (%value = {{var_prefix}}\{{name.var.id}}).nil?
{{var_prefix}}\{{name.var.id}} = \{{yield}}
else
value
%value
end
end
\{% else %}
def {{method_prefix}}\{{name.id}}
if (value = {{var_prefix}}\{{name.id}}).nil?
if (%value = {{var_prefix}}\{{name.id}}).nil?
{{var_prefix}}\{{name.id}} = \{{yield}}
else
value
%value
end
end
\{% end %}
Expand Down Expand Up @@ -561,10 +561,10 @@ class Object
end

def {{method_prefix}}\{{name.var.id}} : \{{name.type}}
if (value = {{var_prefix}}\{{name.var.id}}).nil?
if (%value = {{var_prefix}}\{{name.var.id}}).nil?
::raise ::NilAssertionError.new("\{{@type}}\{{"{{doc_prefix}}".id}}\{{name.var.id}} cannot be nil")
else
value
%value
end
end
\{% else %}
Expand All @@ -573,10 +573,10 @@ class Object
end

def {{method_prefix}}\{{name.id}}
if (value = {{var_prefix}}\{{name.id}}).nil?
if (%value = {{var_prefix}}\{{name.id}}).nil?
::raise ::NilAssertionError.new("\{{@type}}\{{"{{doc_prefix}}".id}}\{{name.id}} cannot be nil")
else
value
%value
end
end
\{% end %}
Expand Down Expand Up @@ -688,18 +688,18 @@ class Object
{{var_prefix}}\{{name.var.id}} : \{{name.type}}?

def {{method_prefix}}\{{name.var.id}}? : \{{name.type}}
if (value = {{var_prefix}}\{{name.var.id}}).nil?
if (%value = {{var_prefix}}\{{name.var.id}}).nil?
{{var_prefix}}\{{name.var.id}} = \{{yield}}
else
value
%value
end
end
\{% else %}
def {{method_prefix}}\{{name.id}}?
if (value = {{var_prefix}}\{{name.id}}).nil?
if (%value = {{var_prefix}}\{{name.id}}).nil?
{{var_prefix}}\{{name.id}} = \{{yield}}
else
value
%value
end
end
\{% end %}
Expand Down Expand Up @@ -970,21 +970,21 @@ class Object
{{var_prefix}}\{{name.var.id}} : \{{name.type}}?

def {{method_prefix}}\{{name.var.id}} : \{{name.type}}
if (value = {{var_prefix}}\{{name.var.id}}).nil?
if (%value = {{var_prefix}}\{{name.var.id}}).nil?
{{var_prefix}}\{{name.var.id}} = \{{yield}}
else
value
%value
end
end

def {{method_prefix}}\{{name.var.id}}=({{var_prefix}}\{{name.var.id}} : \{{name.type}})
end
\{% else %}
def {{method_prefix}}\{{name.id}}
if (value = {{var_prefix}}\{{name.id}}).nil?
if (%value = {{var_prefix}}\{{name.id}}).nil?
{{var_prefix}}\{{name.id}} = \{{yield}}
else
value
%value
end
end

Expand Down Expand Up @@ -1216,21 +1216,21 @@ class Object
{{var_prefix}}\{{name.var.id}} : \{{name.type}}?

def {{method_prefix}}\{{name.var.id}}? : \{{name.type}}
if (value = {{var_prefix}}\{{name.var.id}}).nil?
if (%value = {{var_prefix}}\{{name.var.id}}).nil?
{{var_prefix}}\{{name.var.id}} = \{{yield}}
else
value
%value
end
end

def {{method_prefix}}\{{name.var.id}}=({{var_prefix}}\{{name.var.id}} : \{{name.type}})
end
\{% else %}
def {{method_prefix}}\{{name.id}}?
if (value = {{var_prefix}}\{{name.id}}).nil?
if (%value = {{var_prefix}}\{{name.id}}).nil?
{{var_prefix}}\{{name.id}} = \{{yield}}
else
value
%value
end
end

Expand Down

0 comments on commit 761e253

Please sign in to comment.