Skip to content

n-at-a-time foreach does not print a warning about odd-sized list #20760

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

Closed
leonerd opened this issue Feb 1, 2023 · 7 comments
Closed

n-at-a-time foreach does not print a warning about odd-sized list #20760

leonerd opened this issue Feb 1, 2023 · 7 comments
Labels
Closable? We might be able to close this ticket, but we need to check with the reporter

Comments

@leonerd
Copy link
Contributor

leonerd commented Feb 1, 2023

When assigning to a hash, an even-sized list is required. If an odd-sized list is presented, perl prints a warning:

my %h = (1, 2, 3);
Odd number of elements in hash assignment at (eval 7) line 1.

The new n-at-a-time foreach syntax never gives a warning in similar circumstances:

use experimental 'for_list'; foreach my ($k, $v) (1,2,3) {}
# no warning

Perhaps it should? It can't be worded quite the same way, because "odd vs even" is only a useful concept for n=2. But perhaps some wording such as:

Insufficient elements for final bundle of foreach list at ...
@haarg
Copy link
Contributor

haarg commented Feb 2, 2023

Previous discussion about what the behavior should be: #18925 (comment)

When I was writing the RFC for map/grep with multiple variables, I forgot to check it against what the current behavior of for was. I ended up writing that if there were insufficient elements for the final iteration, a warning should be issued. Depending on what gets decided here, I'll be updating the RFC to match. But throwing warnings for this does seem reasonable.

@iabyn
Copy link
Contributor

iabyn commented Feb 4, 2023 via email

@tobyink
Copy link
Contributor

tobyink commented Feb 18, 2023

Having the list not an exact multiple of the number of variables has documented behaviour, and in many circumstances, that will be reasonable behaviour.

Isn't that also true of my %hash = ( "foo" );, which does warn?

@Grinnz
Copy link
Contributor

Grinnz commented Feb 19, 2023

Yes, but it seems much more likely and useful to intentionally use an uneven list in this case than in hash assignment (particularly since this isn't only for 2 at a time)

@tonycoz
Copy link
Contributor

tonycoz commented Apr 17, 2023

I don't think this should warn, just as my ($x, $y, $z) = (1, 2) doesn't warn, but I can see why there's discussion about it.

@tonycoz
Copy link
Contributor

tonycoz commented Apr 3, 2024

This is closable as WONTFIX I think, based on the discussion on the list (including by @leonerd who opened the ticket.)

@tonycoz tonycoz added the Closable? We might be able to close this ticket, but we need to check with the reporter label Apr 3, 2024
@jkeenan
Copy link
Contributor

jkeenan commented Dec 14, 2024

This is closable as WONTFIX I think, based on the discussion on the list (including by @leonerd who opened the ticket.)

Since April, no one has called for keeping this ticket open. Closing now.

@jkeenan jkeenan closed this as completed Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closable? We might be able to close this ticket, but we need to check with the reporter
Projects
None yet
Development

No branches or pull requests

7 participants