Project

General

Profile

Actions

Feature #21875

open

Handling of trailing commas in lambda parameters

Feature #21875: Handling of trailing commas in lambda parameters

Added by Earlopain (Earlopain _) 10 days ago. Updated 10 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:124797]

Description

https://bugs.ruby-lang.org/issues/19107 was accepted, which is about trailing commands in method definitions.

lambdas were not explicitly mentioned but I wanted to confirm how they should behave with a trailing comma. Or if a trailing comma should even be accepted for them.

It's not clear to me since lambdas sometimes behave like blocks and sometimes more like methods. ->(...) {} for example is syntax invalid (same as in blocks) but they do check their arity with blocks don't do.

If a trailing comma is accepted it can either

  • be implicit splat like in foo do |bar,|; end or foo do |bar|; end. It would also mean that the trailing comma is only allowed after a positional argument.
  • Just be ignored and be accepted in most places like for method definitions.

The first option would be rather useless in regards to https://bugs.ruby-lang.org/issues/19107 when you just want to add the comma for cleaner diffs. But I guess for lambdas this happens very rarely anyways.


Related issues 1 (1 open0 closed)

Related to Ruby - Feature #19107: Allow trailing comma in method signatureOpenprismActions

Updated by Earlopain (Earlopain _) 10 days ago Actions #1

  • Related to Feature #19107: Allow trailing comma in method signature added

Updated by Eregon (Benoit Daloze) 10 days ago · Edited 1Actions #2 [ruby-core:124799]

There is also lambda { |a,| a } which as far as I can tell ignores the trailing comma:

irb(main):015> proc { |a,| a }.call([1,2])
=> 1
irb(main):016> lambda { |a,| a }.call([1,2])
=> [1, 2]
irb(main):017> -> (a,) { a }
<internal:kernel>:168:in 'Kernel#loop': (irb):17: syntax error found (SyntaxError)

irb(main):020> proc { |a,| a }.call(1,2)
=> 1
irb(main):021> lambda { |a,| a }.call(1,2)
(irb):21:in 'block in <top (required)>': wrong number of arguments (given 2, expected 1) (ArgumentError)
irb(main):022> lambda { |a,*| a }.call(1,2)
=> 1

I think since there seems to be no need to change anything for lambdas/procs in this regard I would suggest to change nothing.

It's extremely rare to have a multi-line definition of lambda arguments (parameters), isn't it?
So let's not make the semantics more complicated or incompatible for such a case.

IOW I think you raise a good point and it's sufficiently different from methods to be treated differently.

Updated by Hanmac (Hans Mackowiak) 10 days ago · Edited Actions #3 [ruby-core:124802]

there are functions that look for the amount of parameters that gets spooked by this:

data = {
  'a' => 1,
  'b' => 2,
  'c' => 3
  }

data.each &proc {|a,| p a } #=> "a", "b", "c"

data.each &lambda {|a,| p a } #=>["a", 1], ["b", 2], ["c", 3]
Actions

Also available in: PDF Atom