Get Type in Array

Viewed 502

How can I get the type inside an array?

a = [1,2,3]

I can get the type of a

typeof(a)
Vector{Int64}

but I actually want Int64. First, I thought a newbie work-around could be

typeof(a[1])
Int64

but this is actually not correct, as can be seen here:

a = [1,2,3, missing]

typeof(a)
Vector{Union{Missing, Int64}}

The type of the vector is Union{Missing, Int64}, but the type of the first element is

typeof(a[1])
Int64

So, how do I get the type of the vector/array?

1 Answers

Use the eltype function:

julia> a = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> eltype(a)
Int64

julia> a = [1,2,3, missing]
e4-element Array{Union{Missing, Int64},1}:
 1
 2
 3
  missing

julia> eltype(a)
Union{Missing, Int64}
Related