# A function got exactly the type it expected, so why is it throwing a TypeError?

**URL:** <https://discourse.julialang.org/t/a-function-got-exactly-the-type-it-expected-so-why-is-it-throwing-a-typeerror/3236>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 15, 2017, 3:03pm UTC](https://discourse.julialang.org/t/a-function-got-exactly-the-type-it-expected-so-why-is-it-throwing-a-typeerror/3236 "2017-04-15T15:03:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pegger0709](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pegger0709/32/1811_2.png) [@pegger0709](https://discourse.julialang.org/u/pegger0709)\
**Post date:** [April 15, 2017, 3:03pm UTC](https://discourse.julialang.org/t/a-function-got-exactly-the-type-it-expected-so-why-is-it-throwing-a-typeerror/3236/1 "2017-04-15T15:03:29Z")

</div>

Hello everyone!

I’m having a vexing problem with TypeError that is getting in the way of me producing a large amount of needed data.

Imagine a function foo whose return type is Array{T,2}, and a more complicated function bar whose return type is Array{Tuple{Array{Array{T,2},1},Array{Float64,1}},1}. I’m using parallel processing to produce a 2-dimensional array with dimensions NumberOfRows and NumberOfColumns.

The following code works without problems

> MyFooArray = @parallel hcat for i=1:NumberOfColumns  
> [foo() for j=1:NumberOfRows]  
> end

The following also works OK

> Column\_1 = [bar() for j=1:NumberOfRows]  
> Column\_2 = [bar() for j=1:NumberOfRows]  
> FirstTwoColumns = hcat(Column\_1,Column\_2)

Now here comes the problem. What I really want is not a 2d Array of Foos, but a 2d Array of Bars. In the last example, I produced one without a hitch, so hopefully I can produce a bigger one using a loop, right? Let’s try that

> MyBarArray = @parallel hcat for i=1:NumberOfColumns  
> [bar() for j=1:NumberOfRows]  
> end

When I run this, julia throws the following TypeError

`ERROR: TypeError: collect_to!: in typeassert, expected Array{Tuple{Array{Array{T,2},1},Array{Float64,1}},1}, got Array{Tuple{Array{Array{T,2},1},Array{Float64,1}},1}`

What could be causing this? It got exactly the type it expected.

Thanks so much for your help.

Philip

---

<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:** [April 15, 2017, 6:09pm UTC](https://discourse.julialang.org/t/a-function-got-exactly-the-type-it-expected-so-why-is-it-throwing-a-typeerror/3236/2 "2017-04-15T18:09:57Z")

</div>

Can you please provide your `foo()` and `bar()` functions? Or at least some dummy functions that reproduce the error? It will be much easier to help if we can see a reproducible example.

Also, you can use triple backticks to quote your code like this:

```  
code  
```

---

<div class="post-metadata">

**Author:** ![pegger0709](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pegger0709/32/1811_2.png) [@pegger0709](https://discourse.julialang.org/u/pegger0709)\
**Post date:** [April 15, 2017, 9:03pm UTC](https://discourse.julialang.org/t/a-function-got-exactly-the-type-it-expected-so-why-is-it-throwing-a-typeerror/3236/3 "2017-04-15T21:03:51Z")

</div>

Hi Robin,

As an dummy example of what I meant by foo, let’s say that we have

```julia
MyFooArray = @parallel hcat for i=1:5
   [-rand(20,j)*rand(j,100) for j=1:10]
end

```

What I meant by bar is the function

```julia
function DowkerPersistentintervals(A::Matrix)::Tuple{Array{Array{T,2},1},Array{Float64,1}}

```

which computes the persistent homology intervals of the filtered Dowker complex of a matrix and is part of the Simplicial package

> <https://github.com/nebneuron/Simplicial.jl/blob/master/src/HomologyComputations/PersistenceIntervals.jl>

What works fine is the following

```julia
@everywhere using Simplicial
Column1=[DowkerPersistentintervals(-rand(20,j)*rand(j,100),1,2,"Core_$(myid())_Temp") for j = 1:10];
Column2=[DowkerPersistentintervals(-rand(20,j)*rand(j,100),1,2,"Core_$(myid())_Temp") for j = 1:10];
MySimpleBarArray=hcat(Column1,Column2)

```

Where things go sour is when I do this

```julia
MyBarArray = @parallel hcat for i=1:2
   [DowkerPersistentintervals(-rand(20,j)*rand(j,100),1,2,"Core_$(myid())_Temp") for j=1:10]
end

```

which should give the same result, as MySimpleBarArray, but instead throws

> ERROR: TypeError: collect\_to!: in typeassert, expected Array{Tuple{Array{Array{T,2},1},Array{Float64,1}},1}, got Array{Tuple{Array{Array{T,2},1},Array{Float64,1}},1}

Thanks so much for your help.

---

<div class="post-metadata">

**Author:** ![pegger0709](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pegger0709/32/1811_2.png) [@pegger0709](https://discourse.julialang.org/u/pegger0709)\
**Post date:** [April 17, 2017, 1:16am UTC](https://discourse.julialang.org/t/a-function-got-exactly-the-type-it-expected-so-why-is-it-throwing-a-typeerror/3236/4 "2017-04-17T01:16:23Z")

</div>

Actually, never mind, for some reason it works for large data sets but not small ones. I don’t know why but I won’t complain about it. Consider the matter closed. Thanks anyhow.
