Skip to content

Commit

Permalink
Add #clear method to ArrayLiteral/HashLiteral (crystal-lang#5265)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija authored and RX14 committed Jan 20, 2018
1 parent 6c2297b commit 8bc3cee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/compiler/crystal/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ module Crystal::Macros
def splat(trailing_string : StringLiteral = nil) : MacroId
end

# Similar to `Array#clear`
def clear : ArrayLiteral
end

# Similar to `Array#empty?`
def empty? : BoolLiteral
end
Expand Down Expand Up @@ -625,6 +629,10 @@ module Crystal::Macros

# A hash literal.
class HashLiteral < ASTNode
# Similar to `Hash#clear`
def clear : HashLiteral
end

# Similar to `Hash#empty?`
def empty? : BoolLiteral
end
Expand Down
10 changes: 10 additions & 0 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,11 @@ module Crystal
interpret_argless_method(method, args) { @of || Nop.new }
when "type"
interpret_argless_method(method, args) { @name || Nop.new }
when "clear"
interpret_argless_method(method, args) do
elements.clear
self
end
else
value = intepret_array_or_tuple_method(self, ArrayLiteral, method, args, block, interpreter)
value || super
Expand Down Expand Up @@ -837,6 +842,11 @@ module Crystal
interpret_argless_method(method, args) { @of.try(&.value) || Nop.new }
when "type"
interpret_argless_method(method, args) { @name || Nop.new }
when "clear"
interpret_argless_method(method, args) do
entries.clear
self
end
else
super
end
Expand Down

0 comments on commit 8bc3cee

Please sign in to comment.