# Short circuit evaluation with an assignment statement

**URL:** https://discourse.julialang.org/t/short-circuit-evaluation-with-an-assignment-statement/24443
**Category:** General Usage
**Created:** [May 21, 2019, 4:51pm UTC](https://discourse.julialang.org/t/short-circuit-evaluation-with-an-assignment-statement/24443 "2019-05-21T16:51:18Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)
#### Post date: [May 21, 2019, 4:51pm UTC](https://discourse.julialang.org/t/short-circuit-evaluation-with-an-assignment-statement/24443/1 "2019-05-21T16:51:18Z")

</div>

There is an example in the (increasingly more excellent) manual under short circuit evaluation that shows an assignment statement as the follow-on statement:

```julia
julia> true && (x = (1, 2, 3))
(1, 2, 3)

julia> false && (x = (1, 2, 3))
false

```

The example is clear enough, but the manual doesn’t say why an assignment statement is different than a statement with side effects, say a println(“something”). Just curious about why…

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [May 21, 2019, 4:54pm UTC](https://discourse.julialang.org/t/short-circuit-evaluation-with-an-assignment-statement/24443/2 "2019-05-21T16:54:05Z")

</div>

Not sure what the exact question is but one “difference” is that you need the paranthesis on the right hand side when making an assignment.

---

<div class="post-metadata">

### Author: ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)
#### Post date: [May 21, 2019, 4:55pm UTC](https://discourse.julialang.org/t/short-circuit-evaluation-with-an-assignment-statement/24443/3 "2019-05-21T16:55:41Z")

</div>

The question is WHY are the parens needed for the statement on the right side when it is an assignment statement as opposed to, say, a function call.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [May 21, 2019, 4:58pm UTC](https://discourse.julialang.org/t/short-circuit-evaluation-with-an-assignment-statement/24443/4 "2019-05-21T16:58:36Z")

</div>

Because of presedence. Otherwise it is

`(false && x) = (1,2,3)`

---

<div class="post-metadata">

### Author: ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)
#### Post date: [May 21, 2019, 5:18pm UTC](https://discourse.julialang.org/t/short-circuit-evaluation-with-an-assignment-statement/24443/5 "2019-05-21T17:18:58Z")

</div>

Interesting that = doesn’t have higher precedence than a boolean operator. But, it is what it is and has been since the beginning.

Done.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 21, 2019, 5:31pm UTC](https://discourse.julialang.org/t/short-circuit-evaluation-with-an-assignment-statement/24443/6 "2019-05-21T17:31:32Z")

</div>

It makes sense for assignment to have lower presedence, consider `a = b==c`, you don’t want that to be read as `(a=b) == c`

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [May 21, 2019, 10:24pm UTC](https://discourse.julialang.org/t/short-circuit-evaluation-with-an-assignment-statement/24443/7 "2019-05-21T22:24:38Z")

</div>

Or even more pertinent to this question, you want

```julia
x = y && z

```

to parse as `x = (y && z)` not as `(x = y) && z`. Therefore, `=` must have lower precedence than `&&` and you also have

```julia
x && y = z

```

parsing as `(x && y) = z` rather than `x && (y = z)`. If `&&` and `=` had adjacent precedence levels, you could just put them on the same level and make them left-associative, but they are separated by several precedence levels so that’s not feasible. It might just be possible to break the usual precedence parsing model and give `=` different precedence on the LHS here than on the RHS, but that’s getting into pretty dicey parsing territory.

---

<div class="post-metadata">

### Author: ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)
#### Post date: [May 22, 2019, 12:38am UTC](https://discourse.julialang.org/t/short-circuit-evaluation-with-an-assignment-statement/24443/8 "2019-05-22T00:38:38Z")

</div>

I think parens are fine to be explicit about the order of operation intended: as in nearly every expression. I was lulled into thinking of short circuit evaluation as a way to “run” commands, not as an expression in its own right. As  
the logic table in the documentation makes clear, it is a Boolean expression. It’s just a bit of a trick that we can employ it as a device to run commands as a shortcut to a slightly longer if block. I was merely surprised by the obvious. No serious or  
valid recommendation to change anything here.

Thanks, all.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 22, 2019, 4:21am UTC](https://discourse.julialang.org/t/short-circuit-evaluation-with-an-assignment-statement/24443/9 "2019-05-22T04:21:22Z")

</div>

The presedence and parsing of `=` has been used constructively by many domain-specific languages. Below is an example from Modia that makes use of the fact that julia happily parses any valid expressio on the lhs of `=` (If I’m not mistaken, one of the many reasons Julia was chosen as the host language of Modia)

```julia
@model FirstOrder begin
     x = Variable(start=1) # start means x(0)
     T = Parameter(0.5) # Time constant
     u = 2.0 # Same as Parameter(2.0)
  @equations begin
     T*der(x) + x = u # der() means time derivative
     end
  end;

```
