# Short circuit evaluations documentation when using assignment statement

**URL:** <https://discourse.julialang.org/t/short-circuit-evaluations-documentation-when-using-assignment-statement/47358>\
**Category:** General Usage\
**Created:** [September 27, 2020, 10:53am UTC](https://discourse.julialang.org/t/short-circuit-evaluations-documentation-when-using-assignment-statement/47358 "2020-09-27T10:53:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjmoses](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@rjmoses](https://discourse.julialang.org/u/rjmoses)\
**Post date:** [September 27, 2020, 10:53am UTC](https://discourse.julialang.org/t/short-circuit-evaluations-documentation-when-using-assignment-statement/47358/1 "2020-09-27T10:53:10Z")

</div>

I love the idea of short circuit evaluations. Took me a while to figure out why a piece of code wasn’t working–short circuit evaluation documentation in the Manual section lacks clarity when using an assignment on the right.

Failing code:

n = 1  
n \< 1 && n = 5

Gives:  
ERROR: syntax: invalid assignment location “(n \< 1) && n” around REPL[11]:1  
Stacktrace:  
[1] top-level scope  
@ REPL[11]:1

I would like to see an addition to the short circuit documentation clarifying the use of an assignment on the right side. Something like:

When using an assignment on the right side of a short circuit evaluation, the assignment statement must be enclosed in parenthesis.

n = 0  
n \< 1 && x = 5 # Incorrect

n \< 1 && (x = 5) # Correct

(n \< 1) && (x = 5) # Correct

However, at first glance, the reader might see this as a logical “and” operation and interpret the flow of the statement incorrectly.

I personally would suggest dropping the “||” short circuit and using something such as the keyword “then” in place of the “&&”. In my mind, this leads to a clearer understanding of the code’s function. It might look like:

n \< 1 then x = 5

The false logic can be accommodated by

!(n \< 1) then x = 5

Just thinking…

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 27, 2020, 11:22am UTC](https://discourse.julialang.org/t/short-circuit-evaluations-documentation-when-using-assignment-statement/47358/2 "2020-09-27T11:22:08Z")

</div>

It is more about operator precedence, [Mathematical Operations and Elementary Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity), but a short note about this in the section about short-circuit evaluation, [Control Flow · The Julia Language](https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation), with a simple example would be no harm.

You can easily create a PR to change [https://github.com/JuliaLang/julia/blob/master/doc/src/manual/control-flow.md](https://github.com/JuliaLang/julia/blob/master/doc/src/manual/control-flow.md)

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [September 27, 2020, 11:23am UTC](https://discourse.julialang.org/t/short-circuit-evaluations-documentation-when-using-assignment-statement/47358/3 "2020-09-27T11:23:01Z")

</div>

I learned this the hard way. One way to see how hard it will be for the compiler to figure it out without the parens is a MWE of the thing I broke

```julia
x > 2 || x < -1 || x=4

```

I found the error message unusually informative

```julia
ERROR: syntax: invalid assignment location "(x > 2) || (x < -1) || x" around REPL[8]:1

```

and learned my lesson. I’m pretty happy with the way this works, now that I understand it better.

Agree that the docs could me more clear on this.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 27, 2020, 3:25pm UTC](https://discourse.julialang.org/t/short-circuit-evaluations-documentation-when-using-assignment-statement/47358/4 "2020-09-27T15:25:09Z")

</div>

To me, this looks like a place for a better error message. I’ll see if I can fix it.

---

<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:** [September 28, 2020, 7:52am UTC](https://discourse.julialang.org/t/short-circuit-evaluations-documentation-when-using-assignment-statement/47358/5 "2020-09-28T07:52:56Z")

</div>

> [@rjmoses](#):
>
> suggest dropping the “||” short circuit and using something such as the keyword “then” in place of the “&&”

That’s unlikely to happen, see

> [@PSA: Julia is not at that stage of development anymore](https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872):
>
> You heard about this brand new exciting programming language called Julia and have some suggestions for how it should work. Wonderful! We’re happy to have you here and help you learn the language and listen to your ideas! However, please be aware that “new” is a relative term. Yes, Julia is newer than, say, C, which was first created in 1972 and standardized in 1989. It is not, however, new in the sense that it is still being freely designed and changed in arbitrary ways. Julia development start…

The error message could be improved though.
