# Short-circuit assignment still calling function? // i.e. x |= slowfunc()

**URL:** <https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 1, 2018, 7:02pm UTC](https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802 "2018-08-01T19:02:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 1, 2018, 7:02pm UTC](https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802/1 "2018-08-01T19:02:59Z")

</div>

Let’s say you have a slow function:

```julia
function slowfunc()
  sleep(3)
  true
end

```

Why does the following code make 4 calls to it?

```julia
a = slowfunc()
a |= slowfunc()
a |= slowfunc()
a |= slowfunc()

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [August 1, 2018, 7:13pm UTC](https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802/2 "2018-08-01T19:13:00Z")

</div>

Shouldn’t it be `||` instead of `|`? But I don’t know how to assign to a at the same time.

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [August 1, 2018, 7:19pm UTC](https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802/3 "2018-08-01T19:19:41Z")

</div>

`|` does not short circuit. Its just a plain function. You are looking for `||` which is conceptually different. I don’t think the syntax `||=` is supported, so you would have to write `a = a || slowfunc()`

```julia
julia> |
| (generic function with 16 methods)

julia> ||
ERROR: syntax: invalid identifier name "||"

julia> expand(:(a || b))
:(begin 
        unless a goto 3
        return a
        3: 
        return b
    end)

```

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 1, 2018, 7:28pm UTC](https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802/4 "2018-08-01T19:28:13Z")

</div>

Why would you ever want the behavior described above?

Doesn’t that seem like a performance trap?

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [August 1, 2018, 8:50pm UTC](https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802/5 "2018-08-01T20:50:29Z")

</div>

> [@djsegal](#):
>
> Why would you ever want the behavior described above?

If `x` was `0x0f` and I ran `x |= 0xf0` I’d be really confused if `x` didn’t end up being `0xff`.

I think the main issue is that the function `|` is a different thing than the control flow operator `||`.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 1, 2018, 10:16pm UTC](https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802/6 "2018-08-01T22:16:42Z")

</div>

Is there a github issue explaining why `||=` isn’t a thing?

* * *

**edit:** also, is there a way to treat `|=` as short-circuiting for `Bool` variables?

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [August 2, 2018, 1:16am UTC](https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802/7 "2018-08-02T01:16:20Z")

</div>

short-circuiting can in theory be slower than evaluating all arguments, because the instructions sent to the CPU are less predictable (branch prediction can fail, which ruins the CPU’s pipeline).

I don’t see a reason why `||=` couldn’t be supported…

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 2, 2018, 1:19am UTC](https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802/8 "2018-08-02T01:19:18Z")

</div>

> short-circuiting can in theory be slower than evaluating all arguments, because the instructions sent to the CPU are less predictable (branch prediction can fail, which ruins the CPU’s pipeline).

Not just in theory, in practice too.

It is why a common then to benchmark when optimizing inner loops is  
swapping `a = c ? x() : y()` for `a = ifelse(c, x(), y())`

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 2, 2018, 5:31am UTC](https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802/9 "2018-08-02T05:31:53Z")

</div>

It runs a function 4 times and tells you if it returned true any of those times. Seems useful enough.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 2, 2018, 6:11am UTC](https://discourse.julialang.org/t/short-circuit-assignment-still-calling-function-i-e-x-slowfunc/12802/10 "2018-08-02T06:11:33Z")

</div>

> [@dpsanders](#):
>
> It runs a function 4 times and tells you if it returned true any of those times. Seems useful enough.

I agree with that. I guess I’m just wondering why `||=` got passed over?

This is probably off-topic for a usage question though
