# Convert Vector{AbstractType} to Vector{ConcreteType}

**URL:** <https://discourse.julialang.org/t/convert-vector-abstracttype-to-vector-concretetype/31108>\
**Category:** New to Julia\
**Created:** [November 15, 2019, 12:41am UTC](https://discourse.julialang.org/t/convert-vector-abstracttype-to-vector-concretetype/31108 "2019-11-15T00:41:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Chen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_chen/32/5992_2.png) [@Eric\_Chen](https://discourse.julialang.org/u/Eric_Chen)\
**Post date:** [November 15, 2019, 12:41am UTC](https://discourse.julialang.org/t/convert-vector-abstracttype-to-vector-concretetype/31108/1 "2019-11-15T00:41:58Z")

</div>

How to Convert Vector{AbstractType} to Vector{ConcreteType} ?  
e.g. Vector{Real} to Vector{Int}  
and generate an error if the array contains different concrete types?

e.g. my below example:  
I can only think of one way: First convert to Tuple, then collect back to Vector:  
but this is really dirty, no?

```julia
 abstract type Operate end
 
 struct Cut <: Operate
     member::String
 end
 Cut() = Cut("t")
 
 struct Push <: Operate
     member::Int
 end
 Push() = Push(1)
 
 struct Roll <: Operate
     member::Symbol
 end
 Roll() = Roll(:a)
 
aa = Vector{Operate}(undef, 4)
aa[1] = Roll()
aa[2] = Roll()
aa[3] = Roll()
aa[4] = Roll()

bb = collect(Tuple(aa))

```

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [November 15, 2019, 12:57am UTC](https://discourse.julialang.org/t/convert-vector-abstracttype-to-vector-concretetype/31108/2 "2019-11-15T00:57:05Z")

</div>

```julia
julia> abstract type Operate end

julia> struct Cut <: Operate
           member::String
       end

julia> Cut() = Cut("t")
Cut

julia> struct Push <: Operate
           member::Int
       end

julia> Push() = Push(1)
Push

julia> struct Roll <: Operate
           member::Symbol
       end

julia> Roll() = Roll(:a)
Roll

julia> aa = Vector{Operate}(undef, 4)
4-element Array{Operate,1}:
 #undef
 #undef
 #undef
 #undef

julia> aa[1] = Roll()
Roll(:a)

julia> aa[2] = Roll()
Roll(:a)

julia> aa[3] = Roll()
Roll(:a)

julia> aa[4] = Roll()
Roll(:a)

julia> [a for a ∈ aa]
4-element Array{Roll,1}:
 Roll(:a)
 Roll(:a)
 Roll(:a)
 Roll(:a)

```

If you want to throw an error if they aren’t all of one concrete type, a simple approach is

```julia
julia> function make_concrete(v)
           typeof(first(v))[vᵢ for vᵢ ∈ v]
       end
make_concrete (generic function with 1 method)

julia> make_concrete(aa)
4-element Array{Roll,1}:
 Roll(:a)
 Roll(:a)
 Roll(:a)
 Roll(:a)

julia> aa[3] = Cut()
Cut("t")

julia> make_concrete(aa)
ERROR: MethodError: Cannot `convert` an object of type Cut to an object of type Roll
Closest candidates are:
  convert(::Type{T}, ::T) where T at essentials.jl:170
  Roll(::Any) at REPL[6]:2
Stacktrace:
 [1] setindex!(::Array{Roll,1}, ::Cut, ::Int64)
 [2] make_concrete(::Array{Operate,1})
 [3] top-level scope at REPL[17]:1

```

Note that this wont error if you do define a convert method. If you want it to error anyway, I’d use a loop.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [November 15, 2019, 1:00am UTC](https://discourse.julialang.org/t/convert-vector-abstracttype-to-vector-concretetype/31108/3 "2019-11-15T01:00:15Z")

</div>

You could just use `map(identity, aa)` or `identity.(aa)`

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 15, 2019, 1:20am UTC](https://discourse.julialang.org/t/convert-vector-abstracttype-to-vector-concretetype/31108/4 "2019-11-15T01:20:19Z")

</div>

> [@Eric\_Chen](#):
>
> How to Convert `Vector{AbstractType}` to `Vector{ConcreteType}` ?  
> e.g. `Vector{Real}` to `Vector{Int}`

If you know the type that you want, you could just do `Vector{Int}(array)`.

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

julia> Vector{Int}(a)
3-element Array{Int64,1}:
 1
 2
 3

julia> Vector{Float64}(a)
3-element Array{Float64,1}:
 1.0
 2.0

```

Or, using @Mason’s suggestion:

```julia
julia> identity.(a)
3-element Array{Int64,1}:
 1
 2
 3

```

but this only works if the elements are all actually the same type:

```julia
julia> b = Real[1,2,3.0]
3-element Array{Real,1}:
 1  
 2  
 3.0

julia> identity.(b)
3-element Array{Real,1}:
 1  
 2  
 3.0

julia> Vector{Int}(b)
3-element Array{Int64,1}:
 1
 2
 3

```
