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

Feature request: tuple expansion in blocks #1415

Closed
colinmarc opened this issue Sep 8, 2015 · 5 comments
Closed

Feature request: tuple expansion in blocks #1415

colinmarc opened this issue Sep 8, 2015 · 5 comments

Comments

@colinmarc
Copy link

Sorry if this has already been brought up somewhere.

In ruby, all of the following three snippets of code produce the same output:

arr = (0..10).each_cons(2)
arr.each { |a, b| puts "from #{a} to #{b}" }
(0..10).each_cons(2) { |a, b| puts "from #{a} to #{b}" }
(0..10).each_cons(2) { |tuple| puts "from #{tuple[0]} to #{tuple[1]}" }

Crystal, however, is missing some functionality. The first and third work, but not the second; and the first works only because there's a separate implementation for each_cons (and each_with_index, Hash#each, etc) in order to yield two values.

I'm not sure what exactly the right solution is; but since we automatically compact multiple yield values, as in the third example above, I think it would be a reasonable feature to automatically expand them to the right arity, as well.

@bcardiff
Copy link
Member

bcardiff commented Sep 8, 2015

Hi,

The issue in github is this one
#392
Splat a tuple when yielding is not supported yet.

Feel free to +1 it ;-)

On Tue, Sep 8, 2015 at 10:07 AM, Colin Marc notifications@github.com
wrote:

Sorry if this has already been brought up somewhere.

In ruby, all of the following three snippets of code produce the same
output:

a = (0..10).each_cons(2)
a.each { |a, b| puts "from #{a} to #{b}" }

(0..10).each_cons(2) { |a, b| puts "from #{a} to #{b}" }

(0..10).each_cons(2) { |tuple| puts "from #{tuple[0]} to #{tuple[1]}" }

Crystal, however, is missing some functionality. The first and third work,
but not the second; and the first works only because there's a separate
implementation for each_cons (and each_with_index, Hash#each, etc) in
order to yield two values.

I'm not sure what exactly the right solution is; but since we
automatically compact multiple yield values, as in the third example
above, I think it would be a reasonable feature to automatically expand
them to the right arity, as well.


Reply to this email directly or view it on GitHub
#1415.

Brian J. Cardiff - Manas Technology Solutions
[ar.phone] 4796.0232 #BCR(227)
[us.phone] 312.612.1050 #BCR(227)
[email] bcardiff@manas.com.ar
[web] www.manas.com.ar
[weblog] http://weblogs.manas.com.ar/bcardiff/

@jhass
Copy link
Member

jhass commented Sep 8, 2015

Closing as a duplicate.

@jhass jhass closed this as completed Sep 8, 2015
@colinmarc
Copy link
Author

Cool, I will +1 that one =). FWIW, though, that's not exactly the same as what I'm describing - this would work without splatting anything (as it does in ruby).

@jhass
Copy link
Member

jhass commented Sep 8, 2015

Well, we're pretty aware of autosplat and the lack thereof, just undecided on whether to do that or explicit destructing (#132) ;)

@colinmarc
Copy link
Author

Ah, that's much more what I was getting at. Thanks for the link! =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants