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

Multi-value iteration without parens? #6484

Open
quinnj opened this issue Apr 10, 2014 · 11 comments
Open

Multi-value iteration without parens? #6484

quinnj opened this issue Apr 10, 2014 · 11 comments

Comments

@quinnj
Copy link
Member

quinnj commented Apr 10, 2014

Currently, you have to specify parentheses around multiple values when iterators return multiple values (i.e. Dicts, DataFrame columns, etc.). It'd be great if you didn't have to. Was this explicit for some reason? I couldn't find a discussion on it.

t = Dict()
t["hey"] = 1
t["ho"] = 2
for (k,v) in t
    println("key=$k, value=$v")
end
#errors
for k,v in t
    println("key=$k, value=$v")
end
@JeffBezanson
Copy link
Member

I suspect it's possible, just tricky to parse since you can have for k,v in t, i in 1:n.

@StefanKarpinski
Copy link
Member

It would also conflict with the proposed syntax for variableless for loops – #2675.

@StefanKarpinski
Copy link
Member

On the other hand, I have often done this by accident and wished it worked. It's a toss up.

@johnmyleswhite
Copy link
Member

I often wish this worked as well.

@stevengj
Copy link
Member

This would only save you from typing two characters, so the main question for me is whether it makes the language less surprising / more consistent.

Let's look at what Julia does outside of a loop. On the one hand, (k,v) = foo is the same thing as k,v = foo. On the other hand, the expression (k,v) in foo is inequivalent to k,v in foo. On the gripping hand, usually we only require parentheses when they are necessary to disambiguate parsing, which may or may not be the case here.

For me, it is a toss up, but I am leaning against it.

@milktrader
Copy link
Contributor

+1 in favor of this working. It gives a certain Ruby-esque chic to the code.

@quinnj
Copy link
Member Author

quinnj commented Apr 11, 2014

@stevengj, I think the fact that k,v = foo works is a stronger case because it's semantically the same operation as for k,v in dict, as opposed to (k,v) in foo where I'm checking for containment.
FWIW, python does not require the parentheses.

@bermanmaxim
Copy link

+1 in favor of this, coming from Python I often expect this to work - mandatory parenthesis appears to me as falsely suggesting something related to tuples

@yurivish
Copy link
Contributor

yurivish commented Dec 9, 2017

I think this would be really nice to have as well. I always find myself making the mistake, getting a syntax error, then going back to add parentheses.

@StefanKarpinski
Copy link
Member

Can be done at any point in the 1.x timeframe since it's a non-breaking change.

@andyDoucette
Copy link

andyDoucette commented Oct 30, 2019

I just ran into this issue and ended up here, as I was quite confused as to why what I wrote didn't work. I think it would be much more intuitive if the destructing syntax in a for loop was the same as outside of the for loop. Thus, I'd want "for k,v in t" to work just like "k,v =t" works. Consider this a +1.

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

No branches or pull requests

9 participants