# Initialize AbstractArray?

**URL:** https://discourse.julialang.org/t/initialize-abstractarray/70988
**Category:** General Usage
**Tags:** arrays
**Created:** [November 5, 2021, 5:05am UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988 "2021-11-05T05:05:59Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [November 5, 2021, 5:05am UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/1 "2021-11-05T05:05:59Z")

</div>

Is there a way to initialize an array that is a subtype of `AbstractArray`?  
For example, `x = []` initializes `Vector`s, but how can I initialize an object that can be any arbitrary subtype of `AbstractArray`? is it possible?

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [November 5, 2021, 6:43am UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/2 "2021-11-05T06:43:16Z")

</div>

Can you give a specific example of what you want to achieve?

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [November 5, 2021, 8:26am UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/3 "2021-11-05T08:26:14Z")

</div>

I don’t see an interface method [here](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array) which would allow to do so.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [November 5, 2021, 10:12am UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/4 "2021-11-05T10:12:16Z")

</div>

Not sure if I fully understand the question but from the [manual section on Abstract Types](https://docs.julialang.org/en/v1/manual/types/#man-abstract-types):

> Abstract types cannot be instantiated, and serve only as nodes in the type graph, thereby describing sets of related concrete types: those concrete types which are their descendants.

this combined with

```julia
julia> isabstracttype(AbstractArray)
true

```

makes me think the answer to your question is no, but I might have misunderstood it.

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [November 5, 2021, 11:27am UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/5 "2021-11-05T11:27:28Z")

</div>

Yes, I understand I cannot instantiate an abstract type. This is what I am trying to do:

```julia
function f(x::T) where T <: AbstractArray
    t = typeof(x)
    x0 = t[] # this is meant to be an empty array of type T above
    # some code
    return x0
end

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [November 5, 2021, 11:30am UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/6 "2021-11-05T11:30:24Z")

</div>

Okay, but doesn’t the function you posted already do what you want then? A shorter version of your function is just:

```julia
julia> function f(x::T) where T <: AbstractArray
           T[]
       end
f (generic function with 1 method)

julia> f(rand(2))
Vector{Float64}[]

julia> f(rand(Int, 2))
Vector{Int64}[]

julia> f(5.0)
ERROR: MethodError: no method matching f(::Float64)
Closest candidates are:
  f(::T) where T<:AbstractArray at REPL[43]:1
Stacktrace:
 [1] top-level scope
   @ REPL[46]:100: 

```

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [November 5, 2021, 11:43am UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/7 "2021-11-05T11:43:18Z")

</div>

I think `T[]` creates parametric versions of regular `Array`s, not other subtypes of `AbstractArray`.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [November 5, 2021, 11:46am UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/8 "2021-11-05T11:46:00Z")

</div>

It seems like I’m still misunderstanding what you are trying to do. Can you show an example where the function I posted doesn’t do what you need it to do?

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [November 5, 2021, 11:55am UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/9 "2021-11-05T11:55:42Z")

</div>

This may be a silly example but it illustrates what I am trying to do (and failing):

```julia
julia> using LinearAlgebra

julia> function f(x::T) where T <: AbstractArray
       T[]
       end
f (generic function with 1 method)

julia> x = rand(2, 2)
2×2 Matrix{Float64}:
 0.791934 0.93072
 0.450938 0.813264

julia> xt = transpose(x)
2×2 transpose(::Matrix{Float64}) with eltype Float64:
 0.791934 0.450938
 0.93072 0.813264

julia> xt |> typeof
Transpose{Float64, Matrix{Float64}}

julia> xt isa Transpose
true

julia> xt isa AbstractArray
true

julia> f(xt)
Transpose{Float64, Matrix{Float64}}[]

julia> f(xt) |> typeof
Vector{Transpose{Float64, Matrix{Float64}}} (alias for Array{Transpose{Float64, Array{Float64, 2}}, 1})

julia> f(xt) isa AbstractArray
true

julia> f(xt) isa Transpose
false

julia> f(xt) isa Array
true

```

`T[]` creates regular arrays whose elements are of type `T`, rather than an empty “container” of type `T`. This is the same behavior that creates an array with elements that are floats from `Float64[]`.

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [November 5, 2021, 11:56am UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/10 "2021-11-05T11:56:46Z")

</div>

I feel like you might be asking for the array to be of the type `T`, but this code is actually making a new array that contains the type `T`. Which one is the intended one?

For example `UnitRange` is a subtype of `AbstractArray`, but I don’t think initializing one empty makes much sense so I’m not sure this is a good idea in general (in case you asked for the array to be of type `T`).

EDIT:  
Didn’t see you clarification until after I posted, but it seems like you meant the case I brought up. My feeling is there is no general method to create a meaningful empty collection of the type, though I’m not entirely sure.

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [November 5, 2021, 12:01pm UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/11 "2021-11-05T12:01:41Z")

</div>

Yes, that is what I want. I am trying to figure out a way for the `ParticleSwarm` algorithm of `Optim` to accept `ComponentArrays`. Line 36 of this  
[https://github.com/JuliaNLSolvers/Optim.jl/blob/master/src/multivariate/solvers/zeroth\_order/particle\_swarm.jl#L36](https://github.com/JuliaNLSolvers/Optim.jl/blob/master/src/multivariate/solvers/zeroth_order/particle_swarm.jl#L36)  
initializes the limits with empty “regular” arrays, I was thinking whether there was a way to generalize that to be empty arrays (read containers) of any given subtype of `AbstractArray`.

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [November 5, 2021, 12:04pm UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/12 "2021-11-05T12:04:28Z")

</div>

It is not intrinstically true that all AbstractArrays can have empty instances of them created.

but you might be after `similar`?

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [November 5, 2021, 12:09pm UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/13 "2021-11-05T12:09:55Z")

</div>

I guess it can’t be done then ☹

The problem with `similar` is that it populates the containers.

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [November 5, 2021, 12:12pm UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/14 "2021-11-05T12:12:01Z")

</div>

At least you can give a `dims` argument to `similar`. But I understood you are interested in initializing the new array somehow arbitrarily?

> [@amrods](#):
>
> how can I initialize an object

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [November 5, 2021, 12:14pm UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/15 "2021-11-05T12:14:37Z")

</div>

So am I correct in that you have some `ComponentArray`s that you want to send as the `lower` and `upper` parameters to `ParticleSwarm`?

Depending on exactly what the goal is it could maybe be done like `ParticleSwarm(; lower=collect(lower_component), upper=collect(upper_component))`. This converts the `ComponentArray`s to arrays before sending them.

If you need to send `ComponentArrays` for some reason I don’t think that is possible since `ParticleSwarm` is specifically typed to store a vectore here:  
[https://github.com/JuliaNLSolvers/Optim.jl/blob/master/src/multivariate/solvers/zeroth\_order/particle\_swarm.jl#L2](https://github.com/JuliaNLSolvers/Optim.jl/blob/master/src/multivariate/solvers/zeroth_order/particle_swarm.jl#L2)

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [November 5, 2021, 12:26pm UTC](https://discourse.julialang.org/t/initialize-abstractarray/70988/16 "2021-11-05T12:26:19Z")

</div>

> [@amrods](#):
>
> The problem with `similar` is that it populates the containers.

you can pass in the dims argument to make it of length 0.
