# Method for array of arrays

**URL:** <https://discourse.julialang.org/t/method-for-array-of-arrays/5183>\
**Category:** General Usage\
**Created:** [August 2, 2017, 3:50pm UTC](https://discourse.julialang.org/t/method-for-array-of-arrays/5183 "2017-08-02T15:50:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)\
**Post date:** [August 2, 2017, 3:50pm UTC](https://discourse.julialang.org/t/method-for-array-of-arrays/5183/1 "2017-08-02T15:50:32Z")

</div>

I’d like to write a method for an array of arrays, for example

```julia
simple(g::Array{Array{Float64}}) = g

```

should be an identity function.

But if try

```julia
A = [1. 2.; 3. 4.]
B = [5. 6. 7.; 8. 9. 0.]
g = [A, B]

simple(g)

```

I get

```julia
ERROR: MethodError: no method matching simple(::Array{Array{Float64,2},1})
Closest candidates are:
  simple(::Array{Array{Float64,N} where N,N} where N) at REPL[1]:1

```

If I change the method to

```julia
simple(g::Array{Array{Float64, 2}, 1}) = g

```

I get the expected result, but I’d like to generalize this method to any dimension size. How do I modify my method to do this?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 2, 2017, 3:55pm UTC](https://discourse.julialang.org/t/method-for-array-of-arrays/5183/2 "2017-08-02T15:55:19Z")

</div>

As general as possible on `Array` (of course, this could be extended to `AbstractArray` as well).

```julia
julia> simple(g::Array{Array{T,N},N2}) where {T,N,N2} = g
simple (generic function with 1 method)

julia> A = [1. 2.; 3. 4.]
2×2 Array{Float64,2}:
 1.0 2.0
 3.0 4.0

julia> B = [5. 6. 7.; 8. 9. 0.]
2×3 Array{Float64,2}:
 5.0 6.0 7.0
 8.0 9.0 0.0

julia> g = [A, B]
2-element Array{Array{Float64,2},1}:
 [1.0 2.0; 3.0 4.0]
 [5.0 6.0 7.0; 8.0 9.0 0.0]

julia> simple(g)
2-element Array{Array{Float64,2},1}:
 [1.0 2.0; 3.0 4.0]
 [5.0 6.0 7.0; 8.0 9.0 0.0]

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 2, 2017, 4:00pm UTC](https://discourse.julialang.org/t/method-for-array-of-arrays/5183/3 "2017-08-02T16:00:29Z")

</div>

Or more succinctly: `f(g::Array{<:Array}) = ...`

Or if you want to accept general array-like containers: `f(g::AbstractArray{<:AbstractArray}) = ...`

Note that all of these suggestions will only work in Julia 0.6 or higher (making this possible was a major feature of the 0.6 release)

---

<div class="post-metadata">

**Author:** ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)\
**Post date:** [August 2, 2017, 4:00pm UTC](https://discourse.julialang.org/t/method-for-array-of-arrays/5183/4 "2017-08-02T16:00:51Z")

</div>

Thanks @ChrisRackauckas!

---

<div class="post-metadata">

**Author:** ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)\
**Post date:** [August 2, 2017, 4:01pm UTC](https://discourse.julialang.org/t/method-for-array-of-arrays/5183/5 "2017-08-02T16:01:53Z")

</div>

Thanks @rdeits! Where can I find the documentation about this?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 3, 2017, 1:40am UTC](https://discourse.julialang.org/t/method-for-array-of-arrays/5183/6 "2017-08-03T01:40:07Z")

</div>

There’s some documentation about this feature here: [https://docs.julialang.org/en/stable/manual/types/#UnionAll-Types-1](https://docs.julialang.org/en/stable/manual/types/#UnionAll-Types-1) and in the section above on “Parametric Types”
