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

Splat block arguments not producing a tuple #15373

Open
icy-arctic-fox opened this issue Jan 24, 2025 · 1 comment
Open

Splat block arguments not producing a tuple #15373

icy-arctic-fox opened this issue Jan 24, 2025 · 1 comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:parser

Comments

@icy-arctic-fox
Copy link
Contributor

When using a splat for block arguments with Proc.new, a tuple isn't created. Just the first block argument is assigned.

For example:

add = Proc(Int32, Int32, Int32).new do |*args|
  args.sum
end
puts add.call(1, 2)

results in:

Error: undefined method 'sum' for Int32

Only the first argument is picked up and assigned to args. It is not a tuple.

A more practical example is wrapping a proc where the arguments are unknown.

def wrap(proc : P) : P forall P
  P.new do |*args|
    puts "Before"
    value = proc.call(*args)
    puts "After"
    value
  end
end

proc = ->(x : Int32) { puts x }
wrapped = wrap(proc)
wrapped.call(42)

produces:

Error: argument to splat must be a tuple, not Int32

I've tried this in Crystal 1.14 and 1.15.

Related: #10242
There's something specific about Proc.new that causes this to fail.

@icy-arctic-fox icy-arctic-fox added the kind:bug A bug in the code. Does not apply to documentation, specs, etc. label Jan 24, 2025
@straight-shoota
Copy link
Member

straight-shoota commented Jan 25, 2025

Proc.new isn't anything special, it just captures a block.

Reduced:

def capture(&block : Proc(Int32, Int32, Int32))
  block
end

capture do |*args|
  args.sum # Error: undefined method 'sum' for Int32
end

It seems the parser completely ignores the *. This should rather be a parser error.
Or it should work as expected, which I believe would be feasible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:parser
Projects
None yet
Development

No branches or pull requests

2 participants