# Why no axes(::Tuple)?

Is there a good reason why `axes` (source) isn’t defined for tuples? My (perhaps naive) guess was they would be treated much the same as vectors.

(My reason for wondering is that this is what made me implement my own version of `searchsortedlast` that works for tuples in another question.)

1 Like

Tuples only have one dimension, so I don’t think `axes` make much sense. Is not `eachindex` good for what you are looking for?

2 Likes

It is. But some functions (such as mentioned `searchsortedlast`) depend on `axes`.

And by that logic `axes(::AbstractVector)` doesn’t make much sense either and yet it is that implementation that `searchsortedlast` rely on

This seems like a bigger reason to change `searchsortedlast` than it does to add `axes(::Tuple)`

1 Like

FWIW here are the docstrings

``````axes(A)

Return the tuple of valid indices for array A.

eachindex(A...)

Create an iterable object for visiting each index of an AbstractArray A in an efficient manner
``````

Both seem to be written keeping arrays in mind, and it’s not clear why one should be preferred for indexing `Tuple`s over the other.

The correct function here seems to be `keys`:

``````julia> keys((1,2))
Base.OneTo(2)
``````

which has the docstring

``````keys(iterator)

For an iterator or collection that has keys and values (e.g. arrays and dictionaries), return an iterator over the keys.
``````

This is more general than `axes`.

To me it seems that `axes` works for tuples.

``````julia> a = (1,2,3,4,'a')
(1, 2, 3, 4, 'a')

julia> typeof(a)
Tuple{Int64, Int64, Int64, Int64, Char}

julia> axes(a)
(Base.OneTo(5),)

julia> @which axes(a)
axes(t::Tuple) in Base at tuple.jl:28
``````
3 Likes

Yeah, there’s been a functional `axes(::Tuple)` since Julia v1.1.

3 Likes

I must have done something wrong. Sorry for taking up everybody’s time. It works for me too. I felt sure that I had tested. But I guess that I must have only tested with a tuple in `searchsortedlast`, and that function isn’t defined for tuples. I guess that I should submit a pull request on sort.jl instead.

1 Like

Should this be documented? Currently the docstring for `axes` seems to suggest that it’s only for arrays

2 Likes