# Function with array of matrices as input in Julia 0.5

**URL:** https://discourse.julialang.org/t/function-with-array-of-matrices-as-input-in-julia-0-5/1004
**Category:** General Usage
**Created:** [December 16, 2016, 9:11pm UTC](https://discourse.julialang.org/t/function-with-array-of-matrices-as-input-in-julia-0-5/1004 "2016-12-16T21:11:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lanaperisa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lanaperisa/32/4555_2.png) [@lanaperisa](https://discourse.julialang.org/u/lanaperisa)
#### Post date: [December 16, 2016, 9:11pm UTC](https://discourse.julialang.org/t/function-with-array-of-matrices-as-input-in-julia-0-5/1004/1 "2016-12-16T21:11:34Z")

</div>

Been struggling with something for a while. I use a lot of arrays of matrices so I wanted to create a typealias for it like this:

```julia
typealias MatrixCell{T<:Number} Array{Matrix{T},1}

```

This way I can easily create an array of matrices of predefined length, for example

```julia
A=MatrixCell(2); A[1]=rand(4,4);A[2]=rand(2,2);

```

However, if I define a function like this:

```julia
function test{T<:Number}(A::MatrixCell{T})
       println(typeof(A));
       end

```

calling

```julia
test(A)

```

gives following error:

```julia
ERROR: MethodError: no method matching test(::Array{Array{T<:Number,2},1})
Closest candidates are:
  test{T<:Number}(::Array{Array{T<:Number,2},1}) at REPL[3]:2

```

Now this is really confusing, since the methods match.

I can work around by removing T\<:Number from both MatrixCell and function defintion, but then the function does not work with arrays defined as

```julia
B=[rand(4,3),rand(3,3)]

```

Any ideas? Thanks!

---

<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: [December 16, 2016, 9:35pm UTC](https://discourse.julialang.org/t/function-with-array-of-matrices-as-input-in-julia-0-5/1004/2 "2016-12-16T21:35:41Z")

</div>

This is a corner-case of type-invariance with the current type system. You should find that it behaves as you expect if you give your `MatrixCell` a concrete element type:

```julia
A=MatrixCell{Float64}(2)

```

This is definitely tricky and I think it should be both less surprising and more obvious with the type-system revamp that’s slated for 0.6.

---

<div class="post-metadata">

### Author: ![lanaperisa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lanaperisa/32/4555_2.png) [@lanaperisa](https://discourse.julialang.org/u/lanaperisa)
#### Post date: [December 16, 2016, 9:56pm UTC](https://discourse.julialang.org/t/function-with-array-of-matrices-as-input-in-julia-0-5/1004/3 "2016-12-16T21:56:11Z")

</div>

OK, thanks for the reply. I just realized I can remove T\<:Number from MatrixCell and the function and then define another method of the function to work with arrays defined as

```julia
B=[rand(4,3),rand(3,3)]

```

in the following way:

```julia
test{T<:Number}(A::Array{Matrix{T},1})=test(MatrixCell(A))

```

This way I have the function working for array of matrices defined both ways.  
Sharing always helps 😉
