Bug/unexpected behavior when extending Base.any with new type

This is because in your first case you are imputing a Matrix{Int}, which is not a Matrix{Real}, then another any method is being called. You have to change your function to accept Matrix{<:Real}, which are the matrices for which the elements are any subtypes of Real.

With Matrix{Real} you are asking the function to only accept the matrices that are specifically of type Matrix{Real}, which are matrices that accept, on its elements, any type of real number.

Search for the “covariance” of types in Julia, or for example: Vector{Int} <: Vector{Real} is false??? · JuliaNotes.jl, or Why [1, 2, 3] is not a Vector{Number}?

By the way, you can use:

julia> A = [0 0.0 0 1;
            0 0   0 0;
            0 0   0 1];

julia> any.(==(1), eachrow(A))
3-element BitVector:
 1
 0
 1

(ps: calling your function with the second type of matrix doesn’t work here either, because it is a Matrix{Float64} which is not either a subtype of Matrix{Real}. Your method works, as is, if you explicitly set Real[ 0 0 1; 0 1 0 ; 0 0 1 ], such to get Matrix{Real}).

Finally, the error message (which is not great), is saying it is trying to test if a integer number is true or false, that is, using a integer where it expects a boolean. This occurs because the any function supports not providing the first argument (the function to be mapped to every element of the collection), and in that case it tests for true/false the array itself:

julia> any([true false])
true

julia> any(==(1),[1 0])
true

julia> any([1 0])
ERROR: TypeError: non-boolean (Int64) used in boolean context
2 Likes