Description
Currently, the type of the index returned by the find*
and searchsorted*
functions is kind of inconsistent:
findfirst
etc. (almost) always returns anInt
(unless it returnsnothing
).- the return value
searchsortedfirst(vec, x)
depends on the type ofx
:julia> typeof(searchsortedfirst(1:5, 2)) Int64 julia> typeof(searchsortedfirst(1:5, big(2))) BigInt
The different return types make it harder to write type-stable code. For example, I am writing a function that returns an index of a sorted array, using findlast
in one branch of the function and searchsortedlast
in another.
Another problem with the find*
behavior is that it errors when the indices of the array are not representable as Int
s (but this is a separate issue that should be fixed in keys
or LinearIndices
, cf. #32566)
My proposal: The type returned by find*
and searchsorted*
should only depend on the type of the collection that is being searched. In my opinion, the obvious choice for that type would be eltype(eachindex(collection))
.