JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.53k stars 5.46k forks source link

Multi-value iteration without parens? #6484

Open quinnj opened 10 years ago

quinnj commented 10 years ago

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 commented 10 years ago

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

StefanKarpinski commented 10 years ago

It would also conflict with the proposed syntax for variableless for loops – https://github.com/JuliaLang/julia/issues/2675.

StefanKarpinski commented 10 years ago

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

johnmyleswhite commented 10 years ago

I often wish this worked as well.

stevengj commented 10 years ago

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 commented 10 years ago

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

quinnj commented 10 years ago

@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 commented 9 years ago

+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 commented 6 years ago

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 commented 6 years ago

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

andyDoucette commented 4 years ago

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.