# Precedence of \`=\` vs \`,\`

**URL:** <https://discourse.julialang.org/t/precedence-of-vs/101637>\
**Category:** Internals & Design\
**Tags:** question, syntax\
**Created:** [July 14, 2023, 8:48pm UTC](https://discourse.julialang.org/t/precedence-of-vs/101637 "2023-07-14T20:48:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neal\_Gafter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neal_gafter/32/48152_2.png) [@Neal\_Gafter](https://discourse.julialang.org/u/Neal_Gafter)\
**Post date:** [July 14, 2023, 8:48pm UTC](https://discourse.julialang.org/t/precedence-of-vs/101637/1 "2023-07-14T20:48:38Z")

</div>

The precedence below seems inconsistent. In particular, the last result is unexpected. Is the Julia precedence “whatever the parser does”, or is there some underlying design?

```julia
julia> quote
       a, b
       end
quote
    #= REPL[92]:2 =#
    (a, b)
end

julia> quote
       (a, b)
       end
quote
    #= REPL[93]:2 =#
    (a, b)
end

julia> quote
       (a=1, b=2)
       end
quote
    #= REPL[94]:2 =#
    (a = 1, b = 2)
end

julia> quote
       a=1, b=2
       end
quote
    #= REPL[94]:2 =#
    a = ((1, b) = 2)
end

```

---

<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:** [July 14, 2023, 9:59pm UTC](https://discourse.julialang.org/t/precedence-of-vs/101637/2 "2023-07-14T21:59:23Z")

</div>

If I understand your question correctly, you are wondering why

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

```

returns

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

```

instead of

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

```

Commas are not listed in the [operator precedence](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity) table in the manual, but there is a comment in [`julia-parser.scm`](https://github.com/JuliaLang/julia/blob/22ac24a07fae699d6c820e21971a567ae8f4cd58/src/julia-parser.scm#L10C73-L10C73) that addresses this. The comment says,

> `comma - higher than assignment outside parentheses, lower when inside`

So, since `(a = 1, b = 2)` has the comma inside parentheses, it has lower precedence than the assignments. On the other hand `a = 1, 2` is equivalent to `a = (1, 2)`, because the comma is outside parentheses.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 14, 2023, 11:09pm UTC](https://discourse.julialang.org/t/precedence-of-vs/101637/3 "2023-07-14T23:09:49Z")

</div>

That precedence rule may seem flipflopping for commas but it’s more to do with `=` meaning keyword arguments in parentheses so you want `=` to go first.

```julia
a = 1, 2 # , first - 1 assignment
a = (1, 2) # () first - 1 assignment

a = 1, b = 2 # , first - fails because (1, b) cannot be assigned to
(a = 1, b = 2) # = first - NamedTuple kwarg
a = 1; b = 2 # 2 assignments

a, b = c, d = 1, 2 # , first - 4 assignments
(a, b = c, d = 1, 2) # = first - failed NamedTuple kwarg at 2

```

Be careful though, `=` in parentheses doesn’t always mean it’s a keyword argument, you have to check if it’s in a function call or a NamedTuple construction versus an assignment expression in an operator chain.

```julia
julia> identity(v = 1) # identity kwarg
ERROR: MethodError: no method matching identity(; v=1)
...
julia> identity((v = 1)) # identity kwarg, parentheses do nothing
ERROR: MethodError: no method matching identity(; v=1)
...
julia> identity((v = 1,)) # NamedTuple kwarg needs comma
(v = 1,)
julia> identity((v = 1)+0) # assignment in parentheses near binary operator
1
julia> v
1
julia> identity(+(t = 1, 0)) # + kwarg
ERROR: MethodError: no method matching +(::Int64; t=1)
...
julia> identity(+((t = 1), 0)) # + kwarg, parentheses do nothing
ERROR: MethodError: no method matching +(::Int64; t=1)
...

```
