# Extracting Array element type for subsequent use

**URL:** https://discourse.julialang.org/t/extracting-array-element-type-for-subsequent-use/6840
**Category:** General Usage
**Tags:** question
**Created:** [November 2, 2017, 6:17am UTC](https://discourse.julialang.org/t/extracting-array-element-type-for-subsequent-use/6840 "2017-11-02T06:17:39Z")
**Posts on this page:** 9
**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: [November 2, 2017, 6:17am UTC](https://discourse.julialang.org/t/extracting-array-element-type-for-subsequent-use/6840/1 "2017-11-02T06:17:39Z")

</div>

I want to do something like:

```julia
function foo(x::Array{T,1}, y::Array{T,1})
  n1 = length(x)
  n2 = length(y)
  z = zeros(T, n1+n2)
  ...
end

```

First problem is that I actually need to write

```julia
function foo(x::Array{T,1} where T, y::Array{T,1} where T)
...

```

But then the subsequent use of T gives me an UndefVarError.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [November 2, 2017, 6:38am UTC](https://discourse.julialang.org/t/extracting-array-element-type-for-subsequent-use/6840/2 "2017-11-02T06:38:58Z")

</div>

Use:

```julia
function foo(x::Array{T,1}, y::Array{T,1}) where T
...

```

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [November 2, 2017, 6:39am UTC](https://discourse.julialang.org/t/extracting-array-element-type-for-subsequent-use/6840/3 "2017-11-02T06:39:00Z")

</div>

```julia
function foo{T}(x::Array{T,1}, y::Array{T,1})

```

i think you meant the above .

what you wrote will give you a different T for every value in the arrays I think.

---

<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: [November 2, 2017, 4:12pm UTC](https://discourse.julialang.org/t/extracting-array-element-type-for-subsequent-use/6840/4 "2017-11-02T16:12:03Z")

</div>

That did it, Thank you !

```julia
function foo{T}(x::Array{T,1}, y::Array{T,1}) zeros(T,length(x)+length(y)) end
foo (generic function with 1 method)

```

julia\> foo(rand(10), rand(5))  
15-element Array{Float64,1}:  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0  
0.0

julia\>

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 2, 2017, 4:25pm UTC](https://discourse.julialang.org/t/extracting-array-element-type-for-subsequent-use/6840/5 "2017-11-02T16:25:48Z")

</div>

> [@xiaodai](#):
>
> `function foo{T}(x::Array{T,1}, y::Array{T,1})`

This syntax is going to be removed. Use the `where` version instead.

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [November 2, 2017, 9:04pm UTC](https://discourse.julialang.org/t/extracting-array-element-type-for-subsequent-use/6840/6 "2017-11-02T21:04:02Z")

</div>

That’s the old syntax, @fredrikekre was correct for v0.6 and later.  
That doesn’t not mean that the elements of the Vectors can have a different type, that would be:

```
function foo(x::Vector{Any}, y::Vector{Any})

```

The following means that both x and y have to have the same element type (which can be `Any`)

```
function foo(x::Vector{T}, y::Vector{T}) where T

```

---

<div class="post-metadata">

### Author: ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)
#### Post date: [November 2, 2017, 11:58pm UTC](https://discourse.julialang.org/t/extracting-array-element-type-for-subsequent-use/6840/7 "2017-11-02T23:58:16Z")

</div>

I’ll also point out: if you declare a function like this: `function foo(x::Vector{T} where T)`, you can still obtain the element type of `x` in the function body using the `eltype()` function even though you can’t get it from `T`.

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [November 3, 2017, 12:54am UTC](https://discourse.julialang.org/t/extracting-array-element-type-for-subsequent-use/6840/8 "2017-11-03T00:54:24Z")

</div>

Yes, and there’s even a nice short form:  
`function foo(x::Vector{<:Any})`

---

<div class="post-metadata">

### Author: ![NaOH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naoh/32/5632_2.png) [@NaOH](https://discourse.julialang.org/u/NaOH)
#### Post date: [November 3, 2017, 2:27am UTC](https://discourse.julialang.org/t/extracting-array-element-type-for-subsequent-use/6840/9 "2017-11-03T02:27:27Z")

</div>

The signature should be defined as: `foo(x::Vector{T}) where {T}` instead.

Notice you are indeed able to get the type of the `Vector` from `T` as well as from `eltype(x)`, ie.:

```julia
julia> function foo(x::Vector{T}) where {T}
           @show(T, T == eltype(x))
           return nothing
       end
foo (generic function with 1 method)

julia> a = rand(5);

julia> b = rand(Int, 5);

julia> foo(a)
T = Float64
T == eltype(x) = true

julia> foo(b)
T = Int64
T == eltype(x) = true

```
