-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Override ==, any() and all() for AbstractArray{:>Null} #37
Conversation
This is needed to return null when needed instead of throwing an error. The methods will (and have to) also apply to Array{Any}, but "a little bit type piracy is not the end of the world", at least temporarily.
grumble grumble |
anynull = false | ||
@inbounds for (a, b) in zip(A, B) | ||
eq = (a == b) | ||
if eq === false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't have thought to use ===
for checking booleans before seeing this, but it's noticably faster
julia> @benchmark false === false
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 0.038 ns (0.00% GC)
median time: 0.048 ns (0.00% GC)
mean time: 0.052 ns (0.00% GC)
maximum time: 14.335 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 1000
julia> @benchmark false == false
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 2.353 ns (0.00% GC)
median time: 2.958 ns (0.00% GC)
mean time: 3.835 ns (0.00% GC)
maximum time: 4.157 μs (0.00% GC)
--------------
samples: 10000
evals/sample: 1000
is there any reason to not always use ===
rather than ==
for boolean comparison?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any reason to not always use
===
rather than==
for boolean comparison?
In general, boolean comparisons aren't that common, especially in control flow, since it's typically easier to just say if eq
or if !eq
. In this case ===
is needed to ensure that null
doesn't propagate in the comparison.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't trust benchmarks on elementary operations like this. Wrapping these in a function gives exactly the same native code.
I could also have used isequal(eq, true)
, but using ===
we ensure no other type can override isequal
, as "truthiness" isn't considered as a good idea in general in Base Julia.
This is needed to return null when needed instead of throwing an error.
The methods will (and have to) also apply to
Array{Any}
, but "a little bittype piracy is not the end of the world", at least temporarily.