-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Returntype of searchsorted* should always be the keytype #32568
Comments
Agree that it seems broken for the type of the value being searched for to affect the type of the index returned. |
Yes, this needs to be fixed. Note that this only affects ranges — they do math against the value to compute the result, but we need to convert things back to the correct index type. It should be as easy as throwing a |
In addition to the julia> typeof(findnext(iseven, 1:5, big(1)))
Int64
julia> typeof(findnext(iseven, 1:5, big(2)))
BigInt Should this be fixed as well, or can it be ignored (since most people will probably use an |
Yes, that'd be good to fix at the same time. |
Currently, the type of the index returned by the
find*
andsearchsorted*
functions is kind of inconsistent:findfirst
etc. (almost) always returns anInt
(unless it returnsnothing
).searchsortedfirst(vec, x)
depends on the type ofx
: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 andsearchsortedlast
in another.Another problem with the
find*
behavior is that it errors when the indices of the array are not representable asInt
s (but this is a separate issue that should be fixed inkeys
orLinearIndices
, cf. #32566)My proposal: The type returned by
find*
andsearchsorted*
should only depend on the type of the collection that is being searched. In my opinion, the obvious choice for that type would beeltype(eachindex(collection))
.The text was updated successfully, but these errors were encountered: