Let’s say you have a slow function:
function slowfunc()
sleep(3)
true
end
Why does the following code make 4 calls to it?
a = slowfunc()
a |= slowfunc()
a |= slowfunc()
a |= slowfunc()
Let’s say you have a slow function:
function slowfunc()
sleep(3)
true
end
Why does the following code make 4 calls to it?
a = slowfunc()
a |= slowfunc()
a |= slowfunc()
a |= slowfunc()
Shouldn’t it be ||
instead of |
? But I don’t know how to assign to a at the same time.
|
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> |
| (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)
Why would you ever want the behavior described above?
Doesn’t that seem like a performance trap?
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 ||
.
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?
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…
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())
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