Skip to content

Commit ada0e87

Browse files
authored
Add Proc#[] as alias to #call (#16220)
1 parent a1a8e86 commit ada0e87

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

spec/std/proc_spec.cr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,10 @@ describe "Proc" do
8989
f2.call('r').should eq(2)
9090
end
9191

92+
it "#[]" do
93+
f = ->(x : Int32) { x + 1 }
94+
f[3].should eq(4)
95+
end
96+
9297
typeof(-> { 1 }.hash)
9398
end

src/proc.cr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,16 @@ struct Proc
221221
io << '>'
222222
nil
223223
end
224+
225+
# Invokes this `Proc` and returns the result.
226+
#
227+
# ```
228+
# add = ->(x : Int32, y : Int32) { x + y }
229+
# add[1, 2] # => 3
230+
# ```
231+
#
232+
# Same as `#call`.
233+
def [](*args : *T) : R
234+
call(*args)
235+
end
224236
end

0 commit comments

Comments
 (0)