# IsIndexed trait

**URL:** https://discourse.julialang.org/t/isindexed-trait/91741
**Category:** New to Julia
**Tags:** traits
**Created:** [December 16, 2022, 2:51pm UTC](https://discourse.julialang.org/t/isindexed-trait/91741 "2022-12-16T14:51:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)
#### Post date: [December 16, 2022, 2:51pm UTC](https://discourse.julialang.org/t/isindexed-trait/91741/1 "2022-12-16T14:51:04Z")

</div>

I’m trying to understand is something like a `IsIndexed` trait exists already, or not.

This trait would be true for all things which:

- are collections
- and for each element there is[1] one key which has O(1) access.

The following (and possibly more) stdlib collection interfaces would evaluate to true: [Collections and Data Structures · The Julia Language](https://docs.julialang.org/en/v1/base/collections/#Indexable-Collections), although my definition doesn’t require `firstindex` or `lastindex`.

I think where such a trait would become interesting would be that iterators could use it to decide whether the underlying datastructure supports views: if `IsIndexed(T)` then the element of the iterator should be a view of `T`.

It’s possible that the problem of deciding on whether something should be a view or a copy is already solved generally in a different way and I’m not aware.

[1] exactly one key? possible more? I don’t know.

---

<div class="post-metadata">

### Author: ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)
#### Post date: [April 8, 2023, 1:23pm UTC](https://discourse.julialang.org/t/isindexed-trait/91741/2 "2023-04-08T13:23:36Z")

</div>

+1. I was thinking of something like `IsIndexable`.

I’m pretty sure that nothing like this exists. For example [this excellent blog post](https://bkamins.github.io/julialang/2023/02/03/iterable.html) or the linked doc would have surely mentioned it. It’s puzzling to me that nothing like this exists.

Something like this would be useful

```julia
maybecollect(v) = IsIndexable(v) ? v : collect(v)

```

The following works somewhat:

```julia
struct IndexNotSupported <: IndexStyle
end
Base.IndexStyle(::Type) = IndexNotSupported()
Base.IndexStyle(x) = IndexStyle(typeof(x))

## Tuple is not an AbstractArray, so this is not defined.
## This must break something somewhere.
Base.IndexStyle(::Type{<:Tuple}) = Base.IndexLinear()

maybecollect(v) = IndexStyle(v) === IndexNotSupported() ? collect(v) : v

```

This would say that a `String` is not indexable, which is not true. But a `String` is not efficiently indexable, and `collect` gives you vector of the characters, so maybe that’s ok. [EDIT: this agrees with your definition above]

But it’s probably better to make a new trait.

---

<div class="post-metadata">

### Author: ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)
#### Post date: [April 8, 2023, 8:35pm UTC](https://discourse.julialang.org/t/isindexed-trait/91741/3 "2023-04-08T20:35:09Z")

</div>

Here is a package implements some of these ideas:

> **[GitHub - jlapeyre/InterfaceTraits.jl: Traits for objects and methods in the...](https://github.com/jlapeyre/InterfaceTraits.jl)**
>
> Traits for objects and methods in the "Interfaces" section of the Julia docs - GitHub - jlapeyre/InterfaceTraits.jl: Traits for objects and methods in the "Interfaces" section o...
