Skip to content

Returntype of searchsorted* should always be the keytype #32568

Closed
@sostock

Description

@sostock

Currently, the type of the index returned by the find* and searchsorted* functions is kind of inconsistent:

  • findfirst etc. (almost) always returns an Int (unless it returns nothing).
  • the return value searchsortedfirst(vec, x) depends on the type of x:
    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 Ints (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)).

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedIndicates that a maintainer wants help on an issue or pull request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions