# Pair iteration specification

**URL:** <https://discourse.julialang.org/t/pair-iteration-specification/72794>\
**Category:** New to Julia\
**Tags:** question, syntax\
**Created:** [December 8, 2021, 10:49pm UTC](https://discourse.julialang.org/t/pair-iteration-specification/72794 "2021-12-08T22:49:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [December 8, 2021, 10:49pm UTC](https://discourse.julialang.org/t/pair-iteration-specification/72794/1 "2021-12-08T22:49:54Z")

</div>

What is going on here? Why is this not allowed, and given that it is not allowed, why is there not an error?

```julia
julia> a,b = 1,2
(1, 2)

julia> [a+b for (a=>b) in [3=>4, 5=>6]]
2-element Vector{Int64}:
 3
 3

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 8, 2021, 11:00pm UTC](https://discourse.julialang.org/t/pair-iteration-specification/72794/2 "2021-12-08T23:00:02Z")

</div>

Invalid unpacking syntax:

```julia
julia> (a, b) = 3 => 4
3 => 4

julia> (a => b) = 3 => 4
Error showing value of type UnionAll:
ERROR:
SYSTEM (REPL): showing an error caused an error
ERROR:
SYSTEM (REPL): caught exception of type StackOverflowError while trying to handle a nested exception; giving up

```

edit: that two-line demo is a remarkably innocuous-looking way to create an irrecoverable, unkillable state.

Try

```julia
julia> [a+b for (a, b) in [3=>4, 5=>6]]
2-element Vector{Int64}:
  7
 11

```

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [December 8, 2021, 11:01pm UTC](https://discourse.julialang.org/t/pair-iteration-specification/72794/3 "2021-12-08T23:01:50Z")

</div>

Thanks! I realize now that my initial question was a bit lacking. I’ve edited it to be more specific.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 8, 2021, 11:25pm UTC](https://discourse.julialang.org/t/pair-iteration-specification/72794/4 "2021-12-08T23:25:26Z")

</div>

> [@Lilith](#):
>
> `for (a=>b) in`

where did you see this? Julia is not Haskell/Clojure (I don’t know what language has this pattern matching just guessing here). This is simply not a thing in Julia

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [December 9, 2021, 5:01am UTC](https://discourse.julialang.org/t/pair-iteration-specification/72794/6 "2021-12-09T05:01:50Z")

</div>

> [@stillyslalom](#):
>
> Invalid unpacking syntax:

> [@jling](#):
>
> This is simply not a thing in Julia

If it’s invalid syntax or simply not a thing I’d like to get `ERROR: syntax: invalid iteration specification`. Similarly, does `[8 for f(x) in 1:3]` have any sensical meaning? It evaluates without error to `[8,8,8]`, but `[8 for f(5) in 1:3]` gives an `ERROR: syntax: "5" is not a valid function argument name`.

I feel like your demo is getting at a deeper issue.

```julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _' | |
  | | |_| | | | (_| | | Version 1.7.0 (2021-11-30)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> a => b = error("Hi!") # Let's define a new function
=> (generic function with 1 method) # Only 1 method

julia> 1 => 2 # Oh look, the new method throws an error :)
ERROR: Hi!
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] =>(a::Int64, b::Int64)
   @ Main ./REPL[1]:1
 [3] top-level scope
   @ REPL[2]:1

julia> 1+1 # At least this still works
2

julia> Pair(1, 2) # Wow, Julia internals don't break when I define a new function Main.=>
1 => 2

julia> 

```

whereas

```julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _' | |
  | | |_| | | | (_| | | Version 1.7.0 (2021-11-30)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> 3 => 4 # This line is the only difference between this example and the last. It should have no side effects but...
3 => 4

julia> a => b = error("Hi!") # We try to define a new function but...
Error showing value of type UnionAll:
ERROR: 
SYSTEM (REPL): showing an error caused an error
ERROR: 
SYSTEM (REPL): caught exception of type ErrorException while trying to handle a nested exception; giving up

julia> 1+1 # Wow, this is bad...
┌ Error: Error in the keymap
│ exception =
│ Hi!
│ Stacktrace:
│ [1] error(s::String)
│ @ Base ./error.jl:33
│ [2] Pair(a::Int64, b::Int64)
│ @ Main ./REPL[2]:1
│ [3] _region(s::Union{REPL.LineEdit.MIState, REPL.LineEdit.ModeState, IOBuffer})
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:131
│ [4] region(s::Union{REPL.LineEdit.MIState, REPL.LineEdit.ModeState, IOBuffer})
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:132
│ [5] refresh_multi_line(termbuf::REPL.Terminals.TerminalBuffer, terminal::REPL.Terminals.UnixTerminal, buf::IOBuffer, state::REPL.LineEdit.InputAreaState, prompt::Any; indent::Int64, region_active::Bool)
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:430
│ [6] refresh_multi_line(termbuf::REPL.Terminals.TerminalBuffer, terminal::REPL.Terminals.UnixTerminal, s::Union{REPL.LineEdit.PrefixSearchState, REPL.LineEdit.PromptState}; beeping::Bool)
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:1840
│ [7] refresh_multi_line(termbuf::REPL.Terminals.TerminalBuffer, terminal::REPL.Terminals.UnixTerminal, s::Union{REPL.LineEdit.PrefixSearchState, REPL.LineEdit.PromptState})
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:1839
│ [8] refresh_multi_line(terminal::REPL.Terminals.UnixTerminal, args::Any; kwargs::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}})
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:533
│ [9] refresh_multi_line(terminal::REPL.Terminals.UnixTerminal, args::Any)
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:531
│ [10] refresh_multi_line(s::REPL.LineEdit.PromptState; kw::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}})
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:412
│ [11] refresh_multi_line(s::REPL.LineEdit.PromptState)
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:408
│ [12] refresh_multi_line(::REPL.LineEdit.MIState)
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:227
│ [13] refresh_line(s::Union{REPL.LineEdit.MIState, REPL.LineEdit.ModeState, IOBuffer})
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:1327
│ [14] commit_line(s::REPL.LineEdit.MIState)
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:2131
│ [15] (::REPL.LineEdit.var"#113#166")(::REPL.LineEdit.MIState, ::Any, ::Vararg{Any})
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:2215
│ [16] #invokelatest#2
│ @ ./essentials.jl:716 [inlined]
│ [17] invokelatest
│ @ ./essentials.jl:714 [inlined]
│ [18] (::REPL.LineEdit.var"#25#26"{REPL.LineEdit.var"#113#166", String})(s::Any, p::Any)
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:1466
│ [19] prompt!(term::REPL.Terminals.TextTerminal, prompt::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:2586
│ [20] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
│ @ REPL.LineEdit /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:2488
│ [21] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
│ @ REPL /Applications/Julia-1.7.0.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:1230
│ [22] (::REPL.var"#49#54"{REPL.LineEditREPL, REPL.REPLBackendRef})()
│ @ REPL ./task.jl:423
└ @ REPL.LineEdit /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:2588
julia> 

SYSTEM: caught exception of type :ErrorException while trying to print a failed Task notice; giving up
ERROR: fatal: error thrown and no exception handler available.
ErrorException("Hi!")
error at ./error.jl:33
Pair at ./REPL[2]:1
display_error at ./client.jl:104
unknown function (ip: 0x1297f9448)
jl_apply_generic at /Applications/Julia-1.7.0.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.7.dylib (unknown line)
display_error at ./client.jl:107
unknown function (ip: 0x1297f9044)
jl_apply_generic at /Applications/Julia-1.7.0.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.7.dylib (unknown line)
jl_f__call_latest at /Applications/Julia-1.7.0.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.7.dylib (unknown line)
#invokelatest#2 at ./essentials.jl:716 [inlined]
invokelatest at ./essentials.jl:714 [inlined]
_start at ./client.jl:497
jfptr__start_28060.clone_1 at /Applications/Julia-1.7.0.app/Contents/Resources/julia/lib/julia/sys.dylib (unknown line)
jl_apply_generic at /Applications/Julia-1.7.0.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.7.dylib (unknown line)
true_main at /Applications/Julia-1.7.0.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.7.dylib (unknown line)
jl_repl_entrypoint at /Applications/Julia-1.7.0.app/Contents/Resources/julia/lib/julia/libjulia-internal.1.7.dylib (unknown line)

```

This is either a Julia bug or extremely counterintuitive behavior.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 9, 2021, 5:14am UTC](https://discourse.julialang.org/t/pair-iteration-specification/72794/7 "2021-12-09T05:14:51Z")

</div>

Please file an issue - we should throw sensible errors instead of leaving users in the dark. See:

```julia
julia> 3 + 4
7

julia> a + b = error("Hi!")
ERROR: error in method definition: function Base.+ must be explicitly imported to be extended
Stacktrace:
 [1] top-level scope
   @ none:0
 [2] top-level scope
   @ REPL[5]:1

```

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [December 9, 2021, 5:21am UTC](https://discourse.julialang.org/t/pair-iteration-specification/72794/8 "2021-12-09T05:21:32Z")

</div>

Right! Thanks. This is exactly the warning/error we should have. That (and its presumable fix in Julia) settles your unfortunate demo.

We still have the original questions, though.

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [December 9, 2021, 5:29am UTC](https://discourse.julialang.org/t/pair-iteration-specification/72794/9 "2021-12-09T05:29:11Z")

</div>

> [@stillyslalom](#):
>
> Please file an issue

> <https://github.com/JuliaLang/julia/issues/43379>
>
> \`\`\`
> julia\> 1 =\> 2
> 1 =\> 2
> 
> julia\> (a =\> b) = 1 =\> 2 # Failed attempt to unpac…k a pair
> Error showing value of type UnionAll:
> ERROR: 
> SYSTEM (REPL): showing an error caused an error
> ERROR: 
> SYSTEM (REPL): caught exception of type StackOverflowError while trying to handle a nested exception; giving up
> 
> julia\> ...inconsistent state leading to Julia crash...
> \`\`\`
> Should behave like \`+\` and other builtins:
> \`\`\`
> julia\> 1 =\> 2
> 1 =\> 2
> 
> julia\> (a =\> b) = 1 =\> 2 # Failed attempt to unpack a pair
> ERROR: error in method definition: function Base.=\> must be explicitly imported to be extended
> Stacktrace:
> \[1\] top-level scope
> @ none:0
> \[2\] top-level scope
> @ REPL\[2\]:1
> 
> julia\> ...all is well...
> \`\`\`

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 9, 2021, 7:14am UTC](https://discourse.julialang.org/t/pair-iteration-specification/72794/10 "2021-12-09T07:14:24Z")

</div>

Try this in a fresh Julia session:

```julia
julia> a + b = error("Hi!")
+ (generic function with 1 method)

```

In your case it fails only because you previously used `+` from `Base` in the same session. That’s how it works for all functions, to enable backward compatibility: if the user defines a function and a later version of Julia defines the same function in Base, the user code continues to work.

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [December 9, 2021, 4:00pm UTC](https://discourse.julialang.org/t/pair-iteration-specification/72794/11 "2021-12-09T16:00:42Z")

</div>

The error @stillyslalom produces with `+` is great. We don’t want to accidentally pirate functions from other modules. The bug is that the error is lacking for constructors (e.g. `=>`) so we end up accidentally redefining `Base.=>` which breaks the REPL.
