# Initializing an empty array in an inner constructor with new

**URL:** https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705
**Category:** New to Julia
**Tags:** array, parametric-types, constructors
**Created:** [May 8, 2022, 11:12am UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705 "2022-05-08T11:12:26Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)
#### Post date: [May 8, 2022, 11:12am UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/1 "2022-05-08T11:12:26Z")

</div>

Consider the following struct MyAType

```julia
julia> abstract type AbstractType end

julia> mutable struct MyAType <: AbstractType
          a::Int64
       end

```

When having an Array field of type MyAType in a struct, it can be initialized to an empty array within an inner constructor.

```julia
mutable struct MyArrType
    intArr::Array{MyAType,1}
    MyArrType() = new(MyAType[]) 
end

```

It works:

```julia
julia> a = MyAType(1) 
MyAType(1)

julia> push!(A.intArr,a)
1-element Vector{MyAType}:
 MyAType(1)

```

In contrary, when having a parametric struct:

```julia
mutable struct MyArrType2{T <: AbstractA} 
    intArr::Array{T,1}
    MyArrType2() = new(T[]) 
end
ERROR: syntax: too few type parameters specified in "new{...}" around REPL[4]:1
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

```

The above struct can is functioning by defining an outer constructor:

`MyArrType2{T}() where T <: AbstractType = MyArrayType2(T[])`

> julia\> A2 = MyArrType2{MyAType}()  
> MyArrType2{MyAType}(MyAType)
> 
> julia\> push!(A2.intArr,a)  
> 1-element Vector{MyAType}:  
> MyAType(1)

However, I am wondering why the inner constructor is not working and if there is a possibility to define an inner constructor with the new operator?

---

<div class="post-metadata">

### Author: ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)
#### Post date: [May 8, 2022, 11:58am UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/2 "2022-05-08T11:58:19Z")

</div>

When you write `MyArrType() = new(T[])`, what is the compiler supposed to initialise? An empty array of type `T`? But you haven’t told it what `T` is so it doesn’t know what to do.

The outer one works because you have told it what `T` is explicitly. Perhaps your inner constructor should be `MyArrType{T}() = new(T[])` (but note I haven’t tested this).

---

<div class="post-metadata">

### Author: ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)
#### Post date: [May 8, 2022, 1:24pm UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/3 "2022-05-08T13:24:27Z")

</div>

I did not either tell the compiler what T is when declaring `intArr::Array{T,1}`.

Is not that one of the purposes of parametric types? to be able to write generic code that works for many arbitrary subtypes. I am still assuming that there is a good reason not to allow this with the operator new and there could be a hack to do it in a better way.

---

<div class="post-metadata">

### Author: ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)
#### Post date: [May 8, 2022, 1:34pm UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/4 "2022-05-08T13:34:48Z")

</div>

`intArr::Array{T, 1}` is fine because if you define your struct to be parametric, then as long as you tell the constructor what type you want it to construct then it will be able to do so. The problem with `MyArrType2()` is that you’re not telling the parametric type what `T` is.

`MyArrType2{T}()`? Compiler says “Ok, here is a `intArr::Array{T,1}`”  
`MyArrType2()`? Compiler says “what is `T`?”

---

<div class="post-metadata">

### Author: ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)
#### Post date: [May 8, 2022, 1:39pm UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/5 "2022-05-08T13:39:49Z")

</div>

Yes Indeed. I have tried:

```julia
ulia> mutable struct MyArrType2{T <: AbstractType} 
         intArr::Array{T,1}
         MyArrType2{T}() = new(T[])  
       end
ERROR: UndefVarError: T not defined

```

Edit: However, the following seems to work

```julia
julia> mutable struct MyArrType{T <: AbstractType} 
         intArr::Array{T,1}
         MyArrType{T}() where T <: AbstractType = new(T[])  
       end

```

---

<div class="post-metadata">

### Author: ![tsela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsela/32/52174_2.png) [@tsela](https://discourse.julialang.org/u/tsela)
#### Post date: [May 8, 2022, 1:44pm UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/6 "2022-05-08T13:44:05Z")

</div>

Yes, see [Constructors · The Julia Language](https://docs.julialang.org/en/v1/manual/constructors/#Parametric-Constructors) for more information. But basically, inner constructors are just normal function definitions. You need to write them as you would do if you were outside the struct definition.

The only difference in terms of syntax between inner and outer constructors is that inner constructors have access to the special `new()` function. Otherwise, they use the exact same syntax.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 8, 2022, 1:44pm UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/7 "2022-05-08T13:44:50Z")

</div>

> [@Atiyah](#):
>
> ```julia
> mutable struct MyArrType2{T <: AbstractA} 
> intArr::Array{T,1}
> MyArrType2() = new(T[]) 
> end
> 
> ```

This should be

```julia
mutable struct MyArrType2{T <: AbstractA} 
    intArr::Array{T,1}
    MyArrType2{T}() where T = new{T}(T[]) 
end

```

Because the type parameters are for the _struct_, not its constructor.

---

<div class="post-metadata">

### Author: ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)
#### Post date: [May 8, 2022, 1:48pm UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/8 "2022-05-08T13:48:39Z")

</div>

This is probably what I was missing. I pre-assumed that it is part of the struct.

---

<div class="post-metadata">

### Author: ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)
#### Post date: [May 8, 2022, 1:52pm UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/10 "2022-05-08T13:52:08Z")

</div>

> [@Sukera](#):
>
> ```julia
> mutable struct MyArrType2{T <: AbstractA} 
> intArr::Array{T,1}
> MyArrType2{T}() where T = new{T}(T[]) 
> end
> 
> ```

This code indeed looks precise. Still the code below worked (despite T is not being qualified with the operator new)

```julia
julia> mutable struct MyArrType{T <: AbstractType} 
         intArr::Array{T,1}
         MyArrType{T}() where T <: AbstractType = new(T[])  
       end

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [May 8, 2022, 1:53pm UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/11 "2022-05-08T13:53:05Z")

</div>

> [@Atiyah](#):
>
> ```julia
> julia> mutable struct MyArrType{T <: AbstractType} 
> intArr::Array{T,1}
> MyArrType{T}() where T <: AbstractType = new(T[])  
> end
> 
> ```

```julia
julia> mutable struct MyArrType{T <: AbstractType} 
         intArr::Array{T,1}
         MyArrType{T}() where T = new(T[])  
       end

julia> MyArrType2{MyAType}()
MyArrType2{MyAType}(MyAType[])

```

should just work

---

<div class="post-metadata">

### Author: ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)
#### Post date: [May 8, 2022, 1:55pm UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/12 "2022-05-08T13:55:03Z")

</div>

> [@jling](#):
>
> ```julia
> julia> mutable struct MyArrType{T <: AbstractType} 
> intArr::Array{T,1}
> MyArrType{T}() where T = new(T[])  
> end
> 
> ```

Now you came out with the code with the least redundancies. Now I understand that  
`where T`  
is less redundant than  
`where T <: AbstractType`  
and the operator new does not need to be qualified with the type T

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [May 8, 2022, 1:56pm UTC](https://discourse.julialang.org/t/initializing-an-empty-array-in-an-inner-constructor-with-new/80705/13 "2022-05-08T13:56:33Z")

</div>

> [@Atiyah](#):
>
> less redundant than `where T <: AbstractType`

right, because `T<:AbstractType` is specified in your type definition `MyArrType{T <: AbstractType}` already.

> [@Atiyah](#):
>
> the operator new does not need to be qualified with the type T

in this case it can be inferred because the only field is `intArr` so Julia would know that `T[]` must have type `Array{T, 1}`
