Julia: Multi-value iteration without parens?

Created on 10 Apr 2014  Â·  11Comments  Â·  Source: JuliaLang/julia

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

Most helpful comment

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

All 11 comments

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

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

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

I often wish this worked as well.

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.

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

@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.

+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

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.

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omus picture omus  Â·  3Comments

manor picture manor  Â·  3Comments

StefanKarpinski picture StefanKarpinski  Â·  3Comments

arshpreetsingh picture arshpreetsingh  Â·  3Comments

tkoolen picture tkoolen  Â·  3Comments