# Internals of assignment when doing short circuit evaluation

**URL:** <https://discourse.julialang.org/t/internals-of-assignment-when-doing-short-circuit-evaluation/122178>\
**Category:** General Usage\
**Created:** [November 2, 2024, 2:39pm UTC](https://discourse.julialang.org/t/internals-of-assignment-when-doing-short-circuit-evaluation/122178 "2024-11-02T14:39:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [November 2, 2024, 2:39pm UTC](https://discourse.julialang.org/t/internals-of-assignment-when-doing-short-circuit-evaluation/122178/1 "2024-11-02T14:39:03Z")

</div>

I am doing short circuit evaluation as an if then statement. But I am unsure of how it works so that, even if the second term of the “and” operation is not a boolean value, it works and it returns the value (it executes it instead of evaluating it to its true value:

```julia
true && (n==2)

```

Would be a normal use, but in

```julia
true && (n=2)

```

I wonder how it works. I knowthe parentheses are used to indicate precedence of the assignment

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 2, 2024, 2:54pm UTC](https://discourse.julialang.org/t/internals-of-assignment-when-doing-short-circuit-evaluation/122178/2 "2024-11-02T14:54:52Z")

</div>

> [@martin\_sanchez](#):
>
> But I am unsure of how it works so that, even if the second term of the “and” operation is not a boolean value, it works

`a && b` is equivalent to `a ? b : false`: if `a` is `true` it returns the value of `b` _regardless of whether `b` is boolean_, and if `a` is `false` it returns `false`.

So, `true && (n=2)` is equivalent to `n=2` (which [returns the right-hand side](https://docs.julialang.org/en/v1/manual/variables/#man-assignment-expressions), `2`).

Of course, this will lead to an error if you try to use the result of the `&&` as a boolean value:

```julia
julia> if true && (n=2)
          println("Huh?")
       end
ERROR: TypeError: non-boolean (Int64) used in boolean context

```

The documentation for `&&` should really be clarified on this point: [clarify short-circuit && and || docs by stevengj · Pull Request #56420 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/56420)

---

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [November 2, 2024, 2:57pm UTC](https://discourse.julialang.org/t/internals-of-assignment-when-doing-short-circuit-evaluation/122178/3 "2024-11-02T14:57:22Z")

</div>

Okay, so is it implemented like a?b: false ? Thank you for clarifying. a secondary doubt is, okay, it “returns” (n=2), but it is not a value, but a code. It returns a code to be evaluated. How does Julia handle passing code as a value to be returned and how does it know it needs to evaluate it?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 2, 2024, 3:03pm UTC](https://discourse.julialang.org/t/internals-of-assignment-when-doing-short-circuit-evaluation/122178/4 "2024-11-02T15:03:39Z")

</div>

> [@martin\_sanchez](#):
>
> it “returns” (n=2), but it is not a value, but a code.

Assignment expressions have a value equal to their right-hand-side in Julia. See [Assignment expressions](https://docs.julialang.org/en/v1/manual/variables/#man-assignment-expressions) in the manual.

---

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [November 3, 2024, 5:22pm UTC](https://discourse.julialang.org/t/internals-of-assignment-when-doing-short-circuit-evaluation/122178/5 "2024-11-03T17:22:25Z")

</div>

Thank you. My question goes now to the ` ||` operator. In that case, it also works with non boolean values. How is that implemented? Is it possible to see how is it implemented by myself?

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [November 3, 2024, 5:23pm UTC](https://discourse.julialang.org/t/internals-of-assignment-when-doing-short-circuit-evaluation/122178/6 "2024-11-03T17:23:31Z")

</div>

You can see what it lowers to using `Meta.@lower`, e.g.

```julia
julia> Meta.@lower true && n == 2
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ goto #3 if not true
2 ─ %2 = ==
│ %3 = (%2)(n, 2)
└── return %3
3 ─ return false
))))

julia> Meta.@lower true || n == 2
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = true
└── goto #3 if not %1
2 ─ return %1
3 ─ %4 = ==
│ %5 = (%4)(n, 2)
└── return %5
))))

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 3, 2024, 7:03pm UTC](https://discourse.julialang.org/t/internals-of-assignment-when-doing-short-circuit-evaluation/122178/7 "2024-11-03T19:03:03Z")

</div>

> [@martin\_sanchez](#):
>
> Thank you. My question goes now to the ` ||` operator. In that case, it also works with non boolean values. How is that implemented?

Similar: `x || y` is equivalent to `x ? true : y`.

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [November 3, 2024, 11:59pm UTC](https://discourse.julialang.org/t/internals-of-assignment-when-doing-short-circuit-evaluation/122178/8 "2024-11-03T23:59:13Z")

</div>

Personally I’m not a fan of short-circuit for one-line if. Early on with Julia, it became fashionable, perhaps because it occurs a lot in `Base` and the documentation even advocated for it. Thankfully less recently.

To me a huge selling point of Julia is that it’s readable to non-users almost as pseudo-code. But those unfamiliar with short-circuiting may just go “huh?”

It’s admittedly way less cool to do

```julia
if a; b; end # semi-colons unnecessary but easier to parse

```

but also clearer. If we wanted to be cool there are all sorts of incredible tricks possible with `for` in C, so why even try with Julia?

I imagine even experienced programmers can occasionally brain-freeze and accidentally mix up `||` and `&&`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 4, 2024, 1:48am UTC](https://discourse.julialang.org/t/internals-of-assignment-when-doing-short-circuit-evaluation/122178/9 "2024-11-04T01:48:57Z")

</div>

> [@apo383](#):
>
> Early on with Julia, it became fashionable

It’s an idiom that pre-dates Julia — it’s common in C, and also in `sh` scripting.
