I’m looking at functions with return type Union{T,Nothing}
for some concrete type T
. Most of the time they don’t allocate. But where does the allocation in f(a1, nothing)
in the code below come from? Is there a way to avoid it while keeping the return type of f
as a Union type?
struct A{T}
x::T
end
f(a, b) = iszero(a.x) ? b : a
g(a, b) = iszero(a) ? b : a
a0 = A([0,0])
a1 = A([1,1])
c1 = A(1)
v1 = [1,1]
@ballocated f($a1, nothing) # 16
@ballocated f($a0, nothing) # 0
@ballocated f($a1, $a0) # 0
@ballocated f($c1, nothing) # 0
@ballocated g($v1, nothing) # 0
I’m using Julia 1.8.2.