# Unpacking in arglist vs inside function

**URL:** <https://discourse.julialang.org/t/unpacking-in-arglist-vs-inside-function/103264>\
**Category:** Internals & Design\
**Created:** [August 27, 2023, 8:01pm UTC](https://discourse.julialang.org/t/unpacking-in-arglist-vs-inside-function/103264 "2023-08-27T20:01:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [August 27, 2023, 8:01pm UTC](https://discourse.julialang.org/t/unpacking-in-arglist-vs-inside-function/103264/1 "2023-08-27T20:01:28Z")

</div>

Unpacking inside the function assigns into the parent scope; unpacking in the arglist binds a new variable. Should I expect that from something in [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/) or is that arglist-unpacking a special behavior?

```julia
let x = 1
    function f(θ)
        (;x) = θ
    end

    θ₀ = (;x=2)
    @show x # 1
    f(θ₀)
    @show x # 2
end

let x = 1
    function f((;x))        
    end

    θ₀ = (;x=2)
    @show x # 1
    f(θ₀)
    @show x # 1
end

```

cc @c42f

Edit: [Unpacking in arglist vs inside function · Issue #51107 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/51107)

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 29, 2023, 2:38am UTC](https://discourse.julialang.org/t/unpacking-in-arglist-vs-inside-function/103264/2 "2023-08-29T02:38:01Z")

</div>

Which version are you on? They produce the same results on Julia 1.8.2:

```julia
julia> VERSION
v"1.8.2"

julia> let x = 1
           function f(θ)
               (;x) = θ
           end
       
           θ₀ = (;x=2)
           @show x # 1
           f(θ₀)
           @show x # 2
       end;
x = 1
x = 2

julia> let x = 1
           function f((;x))        
           end
       
           θ₀ = (;x=2)
           @show x # 1
           f(θ₀)
           @show x # 1
       end;
x = 1
x = 2

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [August 29, 2023, 2:39am UTC](https://discourse.julialang.org/t/unpacking-in-arglist-vs-inside-function/103264/3 "2023-08-29T02:39:06Z")

</div>

1.9.1

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [August 30, 2023, 10:16am UTC](https://discourse.julialang.org/t/unpacking-in-arglist-vs-inside-function/103264/4 "2023-08-30T10:16:49Z")

</div>

The behavior changed with 1.9.0, but I’m not sure it was on purpose. It isn’t mentioned in the change log. A git bisect turned up

> <https://github.com/JuliaLang/julia/commit/0c382c245a0d428aae6c593d5def8e6f73dafe34>
>
> This caused const to be dropped from declarations involving named
> tuple destruc…turing.
> 
> Fixes #47168

To be fair, while to be expected if one studies the [manual](https://docs.julialang.org/en/v1/manual/functions/#man-argument-destructuring) thoroughly and grasps the implication of

> If a function argument name is written as a tuple (e.g. `(x, y)`) instead of just a symbol, then an assignment `(x, y) = argument` will be inserted for you:

in conjunction with the scoping rules, I find the fact that in the latter example, the call `f((;x=2))` modifies (rather, used to modify) the parent scope a bit “insane”.

I am glad it has changed, but not too happy about the change being undocumented or possibly unintentional.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 30, 2023, 12:09pm UTC](https://discourse.julialang.org/t/unpacking-in-arglist-vs-inside-function/103264/5 "2023-08-30T12:09:22Z")

</div>

I think the 1.8 behavior is correct and the 1.9 behavior is a bug. The 1.8 behavior is just local scoping rules in action. Admittedly it’s less obvious since the assignment is implicit.
