Is there a way I can define custom method on this "function"?

a variable named a isa Missing or Float64. Which type it exactly belongs to would be known until runtime. So if I want to check

b = 1
a > 1  && b == 1 

I have to:

!ismissing(a)  && a > 1 && b == 1

So is there a way I can do it like:

||(::Missing, ::Bool) = false

a = missing

a > 1 && b == 1
false

this is type piracy, please don’t override Julia’s base function on base type


julia> a ⊗ b = a||b
⊗ (generic function with 1 method)

julia> ⊗(::Missing, ::Bool) = false
⊗ (generic function with 2 methods)

julia> a = missing
missing


julia> (a > 1) ⊗ true
false
3 Likes

This works crecttly, but can not work like the original ||:

a ⊗ b = a||b;

⊗(::Missing, ::Bool) = false

a = missing

true ⊗ (println("function is running"); true)
#true
#function is running"

true || (println("function is running"); true)
#true

well, you can’t have short circuit behavior on a normal function, that’s why you can’t override ||, it’s not just a normal function

1 Like