# How to get the index type of an array \<: AbstractArray?

**URL:** https://discourse.julialang.org/t/how-to-get-the-index-type-of-an-array-abstractarray/4396
**Category:** General Usage
**Tags:** array, type
**Created:** [June 21, 2017, 8:16pm UTC](https://discourse.julialang.org/t/how-to-get-the-index-type-of-an-array-abstractarray/4396 "2017-06-21T20:16:16Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [June 21, 2017, 8:16pm UTC](https://discourse.julialang.org/t/how-to-get-the-index-type-of-an-array-abstractarray/4396/1 "2017-06-21T20:16:16Z")

</div>

A sparse matrix is parameterized by two types: an integer type for the indexes, and a value type. It is trivial to get the type of the latter (`eltype(A)`), but I have not found a portable way of getting the index type that works generically across all arrays.

It can be argued that some arrays (dense arrays in particular) do not have an index type, but in fact maybe they sort of do (it’s probably the system `Int`).

Is there a way to get the type parameterization of the index type of an array that doesn’t rely on the internal representation of the structure?

---

<div class="post-metadata">

### Author: ![joshday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshday/32/368_2.png) [@joshday](https://discourse.julialang.org/u/joshday)
#### Post date: [June 21, 2017, 8:21pm UTC](https://discourse.julialang.org/t/how-to-get-the-index-type-of-an-array-abstractarray/4396/2 "2017-06-21T20:21:38Z")

</div>

I haven’t seen something in Base to do what you’re asking, but I have done this before:

```julia
get_parameter(o::SomeParameterizedType{T}) = T

```

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [June 21, 2017, 8:25pm UTC](https://discourse.julialang.org/t/how-to-get-the-index-type-of-an-array-abstractarray/4396/3 "2017-06-21T20:25:39Z")

</div>

This probably isn’t news to you, but it is always preferable to do

```julia
function f{T}(x::SomeType{T})
    # do stuff with T
end

```

rather than

```julia
function f(x::SomeType)
    T = get_parameter(typeof(x))
    # do stuff with T
end

```

This is because in the former case the compiler knows what `T` is. I suspect that the reason there aren’t lots of generic functions sitting around in `Base` for inferring type parameters is because the devs want to discourage code like my second example. I know that your use case is the index type of sparse matrices, but do you have a specific reason why you don’t want to use the function boundaries to get the type parameter for you?

_ **Addendum:** _ this is incorrect in the particular case of `get_parameter`, see @mbauman’s comment below.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [June 21, 2017, 8:32pm UTC](https://discourse.julialang.org/t/how-to-get-the-index-type-of-an-array-abstractarray/4396/4 "2017-06-21T20:32:27Z")

</div>

@ExpandingMan that’s [not true](https://gist.github.com/mbauman/0ff4719b96334c5237c15ba94df1750b).

Sparse Matrices don’t exploit this in their implementation, but you could use `eltype.(indices(A))`.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [June 21, 2017, 8:42pm UTC](https://discourse.julialang.org/t/how-to-get-the-index-type-of-an-array-abstractarray/4396/5 "2017-06-21T20:42:37Z")

</div>

Hm… now that I think about it I guess that makes sense. I usually think of type parameters introduced in a function body as being run-time variables that aren’t known to the compiler, but I guess if they are in turn being produced by a function where they originated as a type parameter (and are therefore known to the compiler) that isn’t a problem.

Just to expand on this a little: in data applications we are sometimes in the unfortunate position of having datatypes stored in some `Vector{DataType}` which then have to be used later. In these cases, we have to be really careful about function boundaries because the compiler does not know what `DataType` is getting fetched from the `Array`. This leads me to be paranoid so that I’d be really uncomfortable using `get_parameter` even though, as we’ve seen, there’s absolutely nothing wrong with it.

Apologies for the misinformation.
