# Pairs in for loops?

**URL:** <https://discourse.julialang.org/t/pairs-in-for-loops/12895>\
**Category:** Internals & Design\
**Created:** [August 4, 2018, 3:31pm UTC](https://discourse.julialang.org/t/pairs-in-for-loops/12895 "2018-08-04T15:31:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [August 4, 2018, 3:31pm UTC](https://discourse.julialang.org/t/pairs-in-for-loops/12895/1 "2018-08-04T15:31:16Z")

</div>

I see `x => y`-style pairs pop up in new places (e.g., in `replace`), which can help clarify intent, and the roles of different arguments/variables, among other things. I was representing a binary relation as a set of pairs, and without thinking, I wrote a `for` loop that used pair syntax to unpack the elements – as in:

```julia
for (x => y) in R
    ...
end

```

This doesn’t work, of course, but perhaps it should? Even for, say, mapping/dictionary iteration? This is used (though with separate syntax) in PHP, for example, where you can use `foreach (R as $x => $y)`, as opposed to the usual `foreach (R as $p)`.

I’m not arguing that we should be able to unpack to arbitrary `struct`s – but `Pair` is perhaps almost as special as `Tuple`…? I guess it would make sense to permit this as unpacking syntax in general, such as `(x => y) = pop!(R)`; an issue with that is that it’s currently valid syntax…

---

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [August 4, 2018, 3:33pm UTC](https://discourse.julialang.org/t/pairs-in-for-loops/12895/2 "2018-08-04T15:33:50Z")

</div>

(Then again, unpacking arbitrary `struct`s – both in loops and assignments and parameter lists, using constructors that are populated by corresponding fields – would be cool, too. But in this case, we’re even talking about an operator, and not the `Pair` constructor … and unpacking to arbitrary functions would be taking things a _tad_ too far xD)

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [August 4, 2018, 3:37pm UTC](https://discourse.julialang.org/t/pairs-in-for-loops/12895/3 "2018-08-04T15:37:46Z")

</div>

I think the general case here is pattern-matching built in to the language.

I believe Jeff has expressed interest in eventually adding something like this to core Julia, but in the mean time @kevin.squire built a package for pattern matching using macros: [GitHub - kmsquire/Match.jl: Advanced Pattern Matching for Julia](https://github.com/kmsquire/Match.jl).

`Pair` is a plain old struct with the infix `a => b ` as an [alternate spelling](https://github.com/JuliaLang/julia/blob/master/base/pair.jl#L3) of `Pair(a, b)`, so a solution here should probably be more general than treating `Pair` or `=>` specially.

Related to your second point, here’s an example of destructuring a struct: [Home · Match.jl](http://kmsquire.github.io/Match.jl/latest/#Deep-Matching-of-Composite-Types-1).

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [August 4, 2018, 11:36pm UTC](https://discourse.julialang.org/t/pairs-in-for-loops/12895/4 "2018-08-04T23:36:14Z")

</div>

Just to make sure you’re aware, the following syntax does unpack elements:

```julia
for (x, y) in R
    ...
end

```

---

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [August 4, 2018, 11:37pm UTC](https://discourse.julialang.org/t/pairs-in-for-loops/12895/5 "2018-08-04T23:37:24Z")

</div>

Yes, that’s what I’m using 🙂

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 5, 2018, 2:07pm UTC](https://discourse.julialang.org/t/pairs-in-for-loops/12895/6 "2018-08-05T14:07:26Z")

</div>

> [@mlhetland](#):
>
> This doesn’t work, of course, but perhaps it should?

I find the (existing) tuple destructuring syntax better, since it generalizes to 2+ elements naturally. I am not sure that a parallel syntax would be useful for just one special case (n=2).

---

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [August 5, 2018, 2:10pm UTC](https://discourse.julialang.org/t/pairs-in-for-loops/12895/7 "2018-08-05T14:10:07Z")

</div>

It would be specifically for pairs, not iterables in general, I was thinking. But, yeah, just a special case of pattern-matching. I’ll just wait for that maybe showing up at some point 🙂

---

<div class="post-metadata">

**Author:** ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)\
**Post date:** [August 5, 2018, 2:12pm UTC](https://discourse.julialang.org/t/pairs-in-for-loops/12895/8 "2018-08-05T14:12:52Z")

</div>

I guess I formulated that a bit ambiguously. I meant specifically the `Pair` type.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [August 14, 2018, 8:25am UTC](https://discourse.julialang.org/t/pairs-in-for-loops/12895/9 "2018-08-14T08:25:30Z")

</div>

+1  
I would like that too, for consistency. I always find myself having to google for the syntax, as my first attempt is `k => v`.
