# Revisited : passing and returning arrays with unspecified type

**URL:** https://discourse.julialang.org/t/revisited-passing-and-returning-arrays-with-unspecified-type/42859
**Category:** New to Julia
**Created:** [July 10, 2020, 5:51pm UTC](https://discourse.julialang.org/t/revisited-passing-and-returning-arrays-with-unspecified-type/42859 "2020-07-10T17:51:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)
#### Post date: [July 10, 2020, 5:51pm UTC](https://discourse.julialang.org/t/revisited-passing-and-returning-arrays-with-unspecified-type/42859/1 "2020-07-10T17:51:23Z")

</div>

I had [previously](https://discourse.julialang.org/t/passing-arrays-with-unspecified-type/39407/9) asked how to pass arrays of unspecified type as input to a function, to which I had got a nice solution using `where T`. Hoewever, I am having a problem when trying to specify the output type as an Array of unspecified type.  
For example see the following function which accepts a matrix and vector and returns their vector. I am purposefully **not using** `AbstractMatrix` or `AbstractVector` as I eventually will work with higher dimensional arrays.

```julia
function ff( A::AbstractArray{T1, 2} where T1, x::AbstractArray{T2, 1} where T2) ::AbstractArray{T,1} where T
    y = A*x;
    return y;
end

```

I am getting the following error :

```julia
julia> include("t.jl");
julia> TM1 = rand(Int64, 4,3); tv1 = rand(Float64, 3); ff(TM1, tv1)
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] ff(::Array{Int64,2}, ::Array{Float64,1}) at C:\ShobKichhu\Julia\t.jl:2
 [2] top-level scope at none: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: [July 10, 2020, 6:04pm UTC](https://discourse.julialang.org/t/revisited-passing-and-returning-arrays-with-unspecified-type/42859/2 "2020-07-10T18:04:47Z")

</div>

You just need one more set of parentheses to ensure that `AbstractArray{T, 1} where T` is parsed as the return type, rather than trying to apply the `where T` to the entire function definition:

```julia
julia> function ff( A::AbstractArray{T1, 2} where T1, x::AbstractArray{T2, 1} where T2)::(AbstractArray{T,1} where T)
           y = A*x;
           return y;
       end
ff (generic function with 1 method)

julia> ff(zeros(2, 2), zeros(2))
2-element Array{Float64,1}:
 0.0
 0.0

```

It’s also worth noting that the return type annotation will probably have no performance benefit at all, particularly since it’s an abstract type. If it helps you organize and document your code, then that’s totally fine, but you can safely remove it if it makes your life harder.

---

<div class="post-metadata">

### Author: ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)
#### Post date: [July 10, 2020, 6:12pm UTC](https://discourse.julialang.org/t/revisited-passing-and-returning-arrays-with-unspecified-type/42859/3 "2020-07-10T18:12:15Z")

</div>

I see ! Thanks.  
So you mean that if I just wrote  
`function ff() :: AbstractArray`  
it would be the same in terms of performance ? Can I specify just the output array dimension, such as `AbstractArray{1}` ?

---

<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: [July 10, 2020, 6:18pm UTC](https://discourse.julialang.org/t/revisited-passing-and-returning-arrays-with-unspecified-type/42859/4 "2020-07-10T18:18:47Z")

</div>

I mean that if you did `ff()` or `ff()::AbstractArray` or `ff::(AbstractArray{T, 1} where T)` you would get the same performance for any of those.

> [@iamsuddhasattwa](#):
>
> Can I specify just the output array dimension, such as `AbstractArray{1}` ?

That’s exactly what `(AbstractArray{T, 1} where T)` is, and that’s also exactly what `AbstractVector` is:

```julia
julia> AbstractVector === (AbstractArray{T, 1} where T)
true

```

---

<div class="post-metadata">

### Author: ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)
#### Post date: [July 11, 2020, 12:07am UTC](https://discourse.julialang.org/t/revisited-passing-and-returning-arrays-with-unspecified-type/42859/5 "2020-07-11T00:07:13Z")

</div>

Yes, you showed me `AbstractVector` and `AbstractMatrix` before. As I mentioned in this post, I intend to write code that return a 3D matrix. So I guess `AbstractArray{3}` wont work ? I either not mention the type at all, or mention both the eltype and dimension
