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

Implement auto-tuple unpacking in block arguments, Rule 1 #2783

Merged
merged 2 commits into from
Jun 10, 2016

Conversation

asterite
Copy link
Member

@asterite asterite commented Jun 9, 2016

This is similar to #2778 but only Rule 1 (explained there) is implemented.

Additionally, an error is now given if more than expected block arguments are passed, as explained here.

Finally, the code has version checks so that in 0.18.0 Hash will be Enumerable, as well as a few other types (HTTP::Headers, HTTP::Params and ENV). These checks will disappear once we make a new release, but it allows us to avoid having to do an intermediate release that can use the new behaviour.

I personally still like Rule 2 and see no harm in it, specially if combined with the error when extra block arguments are passed. Could be nice for each_with_index and each_with_object, when used on a Hash. But since there aren't many cases of that in the compiler (about ~6) and just a few in other projects I tried, maybe it's not very important.

I'd still like to be able to do hash.each { |key, value| ... } instead of hash.each { |(key, value)| ... }, mostly because the later doesn't add any type safety, just noise. Basically, if I specify more arguments is because I'm expecting a tuple unpack to happen. If that's not the case (it's not a tuple), I get a compile error, so mistakes can't happen. With Rule 2 it's a bit more complicated because there are many more variants to consider.

@asterite asterite force-pushed the feature/block_auto_unpack_rule_1 branch 2 times, most recently from 5b1cc4c to 063277b Compare June 9, 2016 14:38
@asterite
Copy link
Member Author

asterite commented Jun 9, 2016

@ysbaddaden What's your opinion on this?

@ysbaddaden
Copy link
Contributor

Hash will be Enumerable

😍

hash.each_with_index { |(key, value), index| ... }
hash.each_with_object { |(key, value), memo| ... }

Better consistency here!

hash.each { |key, value| ... }

I want to keep it, too. I'd hate hash.each { |(key, value)| }. It may bring more consistency, but it would mean increased noise for no benefit, and be quite unexpected. Nor only for rubyists, but we're iterating a Hash(K, V) not an Array(Tuple(K, V)).

For this same reason, Rule 2 would be nice to have. I don't think it would do any harm, especially with the compile-time checks you propose. If there are exactly as many args as yielded values => no unpacking, if there are more args than yielded values => try to unpack, if there are too many args available than values to unpack => compile error (broken expectation) ; if there are too few args available than values to unpack => compile error (can't fit).

Ary Borenszweig added 2 commits June 9, 2016 19:54

Verified

This commit was signed with the committer’s verified signature. The key has expired.
@asterite asterite force-pushed the feature/block_auto_unpack_rule_1 branch from 063277b to ca12dce Compare June 9, 2016 22:54
@asterite
Copy link
Member Author

Let's start with rule 1 and later see if rule 2 turns out to be convenient or not.

@asterite asterite merged commit 9c135ad into master Jun 10, 2016
@asterite asterite deleted the feature/block_auto_unpack_rule_1 branch June 10, 2016 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants