My wishlist for the next version of Julia

Not sure if this was answered already, but I think it would still short-circuit in broadcasting. Just not the way that people wrote above.

In this example, I don’t think you’d see anything printed, although I can’t test as I’m still on an older Julia version.

function f(x)
    println("f was called")
    isodd(x)
end

x = [false, false, false]
y = [1, 2, 3]
z = x .&& f.(y)

I think this should be the same as this, so it shouldn’t execute f:

broadcast(x, y) do _x, _y
    _x && f_(y)
end

The examples above tried to short-circuit before the array arguments were even created, which doesn’t work.

In 1.7:

julia> f(x) = x
f (generic function with 2 methods)

julia> x = [true,false,true];

julia> x .&& f.([1,2,3])
3-element Vector{Integer}:
     1
 false
     3


or

julia> f(x) = println("I'm here $x")
f (generic function with 2 methods)

julia> x .&& f.([1,2,3])
I'm here 1
I'm here 3
3-element Vector{Union{Nothing, Bool}}:
      nothing
 false
      nothing


2 Likes

Yup, there’s the short circuiting

You’re missing a dot.

julia> [false, false] .&& error.("not allowed")
2-element BitVector:
 0
 0

When you write [false, false] .&& error("not allowed") that’s a little like writing

A = [false, false]
b = error("not allowed")
C = BitVector(undef, 2) 
for i in eachindex(A)
    C[i] = A[i] && b
end

whereas when you write [false, false] .&& error("not allowed") that instead becomes something like

A = [false, false]
C = BitVector(undef, 2) 
for i in eachindex(A)
    C[i] = A[i] && error("not allowed")
end
4 Likes

.&& just became much more intersting to me!

I will update my previous answers

3 Likes