# Trouble with Internal Constructor

**URL:** <https://discourse.julialang.org/t/trouble-with-internal-constructor/66094>\
**Category:** New to Julia\
**Created:** [August 9, 2021, 9:12pm UTC](https://discourse.julialang.org/t/trouble-with-internal-constructor/66094 "2021-08-09T21:12:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_becker/32/10221_2.png) [@Jeff\_Becker](https://discourse.julialang.org/u/Jeff_Becker)\
**Post date:** [August 9, 2021, 9:12pm UTC](https://discourse.julialang.org/t/trouble-with-internal-constructor/66094/1 "2021-08-09T21:12:43Z")

</div>

I’m trying to do the following:

julia\> mutable struct CustomSet{T}  
elements:: Vector{T}  
CustomSet(e) = new{CustomSet{T}}(sort(e))  
end

I expect the inner constructor to return a new CustomSet with the Vector e sorted. But it doesn’t work.

julia\> c = CustomSet([1,2,4,3])

CustomSet{Int64}([1, 2, 4, 3])

What am I doing wrong? Thanks.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 9, 2021, 9:19pm UTC](https://discourse.julialang.org/t/trouble-with-internal-constructor/66094/2 "2021-08-09T21:19:38Z")

</div>

You want `new{T}(sort(e)`, not `new{CustomSet{T}}(sort(e))`. The parameters in `{}` for `new` are the same as the _parameters_ of your type, not the type with its parameters. Otherwise you’re asking `new` to give you a `CustomSet{CustomSet{T}}`

Edit: for example, here’s a working implementation:

```julia
julia> mutable struct CustomSet{T}
         elements:: Vector{T}
         CustomSet(e::AbstractVector{T}) where {T} = new{T}(sort(e))
       end

julia> CustomSet([1,4,2,3])
CustomSet{Int64}([1, 2, 3, 4])

```

and here’s a slightly more general version:

```julia
julia> mutable struct CustomSet{T}
         elements:: Vector{T}
         CustomSet(e) = new{eltype(e)}(sort(e))
       end

julia> CustomSet([1,4,2,3])
CustomSet{Int64}([1, 2, 3, 4])

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 9, 2021, 9:21pm UTC](https://discourse.julialang.org/t/trouble-with-internal-constructor/66094/3 "2021-08-09T21:21:22Z")

</div>

Welcome back! Three things here:

1. The struct definition you posted doesn’t behave like you posted. I see an error (`UndefVarError: T not defined`). I think you have leftover method definitions from earlier attempts.

2. The parameterized `new{...}` takes _just_ the type parameter for the struct. (Ah, yes, what Robin beat me to above)

3. You need to tell Julia what that `T` is going to be — it doesn’t magically grab it for you in this case. So you want `new{eltype(e)}`.

So all together:

```julia
julia> mutable struct CustomSet{T}
           elements:: Vector{T}
           CustomSet(e) = new{eltype(e)}(sort(e))
       end

julia> CustomSet([1,2,4,3])
CustomSet{Int64}([1, 2, 3, 4])

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_becker/32/10221_2.png) [@Jeff\_Becker](https://discourse.julialang.org/u/Jeff_Becker)\
**Post date:** [August 9, 2021, 9:22pm UTC](https://discourse.julialang.org/t/trouble-with-internal-constructor/66094/4 "2021-08-09T21:22:41Z")

</div>

Thanks - but that has the same result.

julia\> mutable struct CustomSet{T}  
elements:: Vector{T}  
CustomSet(e) = new{T}(sort(e))  
end

julia\> c = CustomSet([1,2,4,3])  
CustomSet{Int64}([1, 2, 4, 3])

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 9, 2021, 9:25pm UTC](https://discourse.julialang.org/t/trouble-with-internal-constructor/66094/5 "2021-08-09T21:25:52Z")

</div>

> [@mbauman](#):
>
> I think you have leftover method definitions from earlier attempts.

This is one of those cases where you may need to restart Julia. You can also look into using Revise, but iterating on struct definitions isn’t its strong suit.

You can probably see what’s happening with `methods(CustomSet)`. I’m guessing you have some outer constructors that are taking precedence.

---

<div class="post-metadata">

**Author:** ![Jeff\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_becker/32/10221_2.png) [@Jeff\_Becker](https://discourse.julialang.org/u/Jeff_Becker)\
**Post date:** [August 9, 2021, 9:28pm UTC](https://discourse.julialang.org/t/trouble-with-internal-constructor/66094/6 "2021-08-09T21:28:30Z")

</div>

Still doesn’t work (using Julia 1.62)

julia\> mutable struct CustomSet{T}  
elements:: Vector{T}  
CustomSet(e) = new{eltype(e)}(sort(e))  
end

julia\> c = CustomSet([1,2,4,3])  
CustomSet{Int64}([1, 2, 4, 3])

---

<div class="post-metadata">

**Author:** ![Jeff\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_becker/32/10221_2.png) [@Jeff\_Becker](https://discourse.julialang.org/u/Jeff_Becker)\
**Post date:** [August 9, 2021, 9:33pm UTC](https://discourse.julialang.org/t/trouble-with-internal-constructor/66094/7 "2021-08-09T21:33:11Z")

</div>

Restarting Julia, and retrying works. Thanks.
