# Why doesn't empty array match any type signature for array?

**URL:** https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465
**Category:** New to Julia
**Created:** [August 24, 2020, 5:59pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465 "2020-08-24T17:59:54Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [August 24, 2020, 5:59pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/1 "2020-08-24T17:59:55Z")

</div>

```julia
function foo(x::Tuple{Array{Float64,2}, String, Bool})
    a, s, b = x
    if a != []
        println(a)
    end
end

function main()
    foo(([], "", false))
end

main()

ERROR: LoadError: MethodError: no method matching foo(::Tuple{Array{Any,1},String,Bool})

```

I’ll just type annotate the empty array…

```julia
function main()
    foo(([]::Array{Float64,2}, "", false))
end

ERROR: LoadError: TypeError: in typeassert, expected Array{Float64,2}, got Array{Any,1}

```

I get this problem a lot…  
Strangely i always want to solve it my annotating the type in the caller like I’ve done, and everytime i do that i remember that i can’t do that. it’s seems very natural, am i getting Julia mixed up with another language ? 😆  
I eventually solve it by removing the declaration, but I’m not going to do that any more. Some kind soul on the Julia forum is going to show me the error in my ways 🙂

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 24, 2020, 6:08pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/2 "2020-08-24T18:08:52Z")

</div>

First, this specificity of types of the function parameters is probably not needed.

But if you insist, the error is quite clear:  
`[]` is not of type `Array{Float64,2}` so it doesn’t match.  
What you need is a `Array{Float64,2}`, e.g.:

```julia
foo( (Array{Float64,2}([[] []]), "", false))

```

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 24, 2020, 6:13pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/3 "2020-08-24T18:13:28Z")

</div>

A proper array constructor would be:  
`Array{Float64}(undef,0,2)`

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [August 24, 2020, 6:17pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/4 "2020-08-24T18:17:01Z")

</div>

I would suggest:

```julia
foo((Float64[[] []], "", false))

```

Unless you actually want an array of arrays.

The `typessert` does not work as you think it works. It will try to convert and throw an exception if it is wrong, and if used in a binding (not a literal) it will force that binding to only accept values of that type. It will not, however, serve to define which is the element type of the array literal. In such case, you declare the array as `Type[...]` for an array of type `Type` and contents `...`.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 24, 2020, 6:17pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/5 "2020-08-24T18:17:50Z")

</div>

Alternatively:

```julia
julia> function foo(x::Tuple{AbstractArray, String, Bool})
           a, s, b = x
           if a != []
               println(a)
           end
       end

julia> foo(([], "", false))

```

---

<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: [August 24, 2020, 6:19pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/6 "2020-08-24T18:19:08Z")

</div>

Yes, widening signatures like that is almost always the best answer. Not only will that work with `[]`, it’ll also make your function just work for things like `Diagonal`s, static arrays, sparse matrices, etc., etc.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [August 24, 2020, 6:19pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/7 "2020-08-24T18:19:21Z")

</div>

> [@oheil](#):
>
> First, this specificity of types of the function parameters is probably not needed.

not needed, but desirable. they this came up when I tried to pass an Array{Float64,1} instead of Array{Float64,2} and spent the next 5 minutes trying to figure out why i was getting an error message…

also,

```julia-auto
x=Array{Float64}(undef,0,2)
x==[]
false

```

---

<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: [August 24, 2020, 6:19pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/8 "2020-08-24T18:19:52Z")

</div>

Check `isempty` instead of comparing to an empty vector.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 24, 2020, 6:23pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/9 "2020-08-24T18:23:10Z")

</div>

Yes, sometimes. I tend in a current project to the same because making the types explicit helps me to recognize what the function has to do somehow. The speaking name together with the parameter types does help me to be more productive.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [August 24, 2020, 6:26pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/10 "2020-08-24T18:26:48Z")

</div>

> [@Henrique\_Becker](#):
>
> I would suggest:

```julia
function main()
    foo((Array{Float64,2}[], "", false))
end
ERROR: LoadError: MethodError: no method matching foo(::Tuple{Array{Array{Float64,2},1},String,Bool})

```

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [August 24, 2020, 6:30pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/11 "2020-08-24T18:30:55Z")

</div>

> [@purplishrock](#):
>
> `(Array{Float64,2}[], "", false)`

Yes, I was wrong about the notation, the `[[] []]` is actually needed, but the correct form is `typeof((Float64[[] []], "", false))` not the one presented by @oheil, I will edit the original.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [August 24, 2020, 6:34pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/12 "2020-08-24T18:34:49Z")

</div>

```julia
julia> typeof((Float64[[][]], "", false))
ERROR: BoundsError: attempt to access 0-element Array{Any,1} at index []
Stacktrace:
 [1] throw_boundserror(::Array{Any,1}, ::Tuple{}) at .\abstractarray.jl:537
 [2] checkbounds at .\abstractarray.jl:502 [inlined]
 [3] _getindex at .\abstractarray.jl:1002 [inlined]
 [4] getindex(::Array{Any,1}) at .\abstractarray.jl:980
 [5] top-level scope at REPL[1]:1

```

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [August 24, 2020, 6:38pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/13 "2020-08-24T18:38:59Z")

</div>

I cannot reproduce this last error, in Julia 1.4.2 I have:

```julia
julia> typeof((Float64[[] []], "", false))
Tuple{Array{Float64,2},String,Bool}
 

```

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [August 24, 2020, 6:43pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/14 "2020-08-24T18:43:37Z")

</div>

OH! if you look very carefully at my example you’ll see that i’m missing a space !!  
so tricksy…

my bad, thank you for your solution 🙂

haha. in looking at your original reply , the line split right between the two ‘[]’ elements. lol. that’s why i didn’t realize there was a space there. Good to know that the space is significant.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [August 24, 2020, 7:38pm UTC](https://discourse.julialang.org/t/why-doesnt-empty-array-match-any-type-signature-for-array/45465/15 "2020-08-24T19:38:25Z")

</div>

Oh, yes, spacing is significant in Julia (while not so much like Python indentation). I recommend always copying an entire block of code, instead of copying until the end of the line, and then the rest, to avoid such problems.
