# Is this a ternary (?) operator bug?

**URL:** <https://discourse.julialang.org/t/is-this-a-ternary-operator-bug/99998>\
**Category:** General Usage\
**Created:** [June 7, 2023, 12:15pm UTC](https://discourse.julialang.org/t/is-this-a-ternary-operator-bug/99998 "2023-06-07T12:15:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [June 7, 2023, 12:15pm UTC](https://discourse.julialang.org/t/is-this-a-ternary-operator-bug/99998/1 "2023-06-07T12:15:26Z")

</div>

```julia
julia> function foo(a,b) a *= "+1"; b *= "+2"; return a,b end

julia> function baz() a="";b=""; (true) ? a,b = foo("","") : nothing; println(a,b) end
ERROR: syntax: colon expected in "?" expression
Stacktrace:
 [1] top-level scope
   @ none:1

```

So it doesn’t like something in the first branch (that with the `a,b = foo("","")` so lets wrap it in parenthesis.

```julia
julia> function baz() a="";b=""; (true) ? (a,b) = foo("","") : nothing; println(a,b) end

julia> baz()
+1+2

```

Fine, now it works. But why doesn’t it work if we instead do `(a,b = foo("",""))`?  
Note that this was the real case that broke my code and made spend quite some time debugging it.

```julia
julia> function baz() a="";b=""; (true) ? (a,b = foo("","")) : nothing; println(a,b) end

julia> baz()

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 7, 2023, 12:22pm UTC](https://discourse.julialang.org/t/is-this-a-ternary-operator-bug/99998/2 "2023-06-07T12:22:43Z")

</div>

> [@joa-quim](#):
>
> (a,b = foo(“”,“”))

Julia thinks this is a NamedTuple definition, and doesn’t assign anything to `a` or `b`.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [June 7, 2023, 12:32pm UTC](https://discourse.julialang.org/t/is-this-a-ternary-operator-bug/99998/3 "2023-06-07T12:32:26Z")

</div>

Thanks. The problem is that in my real case code not even the `(a,b) = foo("","")` form worked and I had to break it in `if else end`. But I’m not able to reproduce it with this synthetic example.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 7, 2023, 12:56pm UTC](https://discourse.julialang.org/t/is-this-a-ternary-operator-bug/99998/4 "2023-06-07T12:56:30Z")

</div>

I think the most flexible form, for the case at hand, is the following

```julia
function baz(boool) a="a";b="b"; a, b = (boool) ? foo(a,b) : (nothing,nothing); println(a,'\n',b) end
baz(true)
baz(false)

```

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [June 7, 2023, 1:03pm UTC](https://discourse.julialang.org/t/is-this-a-ternary-operator-bug/99998/5 "2023-06-07T13:03:30Z")

</div>

No because `a, b = (bool) ? foo(a,b) : (nothing,nothing)` would always change `a,b`, which is not the case in my example (and specially in my real case code)
