Function with array of matrices as input in Julia 0.5


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:

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

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

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

However, if I define a function like this:

function test{T<:Number}(A::MatrixCell{T})



gives following error:

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


Any ideas? Thanks!


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:


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.


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


in the following way:


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