# Can this be done with an outer constructor?

**URL:** <https://discourse.julialang.org/t/can-this-be-done-with-an-outer-constructor/39955>\
**Category:** New to Julia\
**Created:** [May 22, 2020, 11:14am UTC](https://discourse.julialang.org/t/can-this-be-done-with-an-outer-constructor/39955 "2020-05-22T11:14:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [May 22, 2020, 11:14am UTC](https://discourse.julialang.org/t/can-this-be-done-with-an-outer-constructor/39955/1 "2020-05-22T11:14:23Z")

</div>

I recently had a desire for this type of convenience constructor:

```julia
# Some generic wrapper
struct AA{T}
       a::T
end
# Create a new wrapper of same inner type
AA{T}(a...) where T = AA(T(a...))

# Dummy to wrap
struct AAA
    a
end

julia> aa = AA(AAA(3))
AA{AAA}(AAA(3))

julia> at = typeof(aa)
AA{AAA}

julia> at(5) # Does not seem to be dispatched to the outer constructor :(
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type AAA
Closest candidates are:
  convert(::Type{T}, ::T) where T at essentials.jl:171
  AAA(::Any) at none:3
Stacktrace:
 [1] AA{AAA}(::Int64) at .\none:3
 [2] top-level scope at none:0

```

It works with inner constructors though:

```julia
struct BB{T}
    a::T

    BB(a::T) where T = new{T}(a)
    BB{T}(a...) where T = new{T}(T(a...))
end

julia> bb = BB(AAA(3))
BB{AAA}(AAA(3))

julia> bt = typeof(bb)
BB{AAA}

julia> bt(5)
BB{AAA}(AAA(5))

```

Anything missing to make the outer constructor case work?

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [May 22, 2020, 12:15pm UTC](https://discourse.julialang.org/t/can-this-be-done-with-an-outer-constructor/39955/2 "2020-05-22T12:15:44Z")

</div>

[This is how it is handled by the docs](https://docs.julialang.org/en/v1/manual/constructors/#Outer-only-constructors-1). Basically you have to suppress the default constructors somehow…

```julia

struct AA{T} 
    a::T
    function AA(a::T) where T
        new{T}(a)
    end
end
# Create a new wrapper of same inner type
AA{T}(a...) where T = AA(T(a...))

# Dummy to wrap
struct AAA
    a
end

julia> aa = AA(AAA(3))
AA{AAA}(AAA(3))

julia> at = typeof(aa)
AA{AAA}

julia> at(5)
AA{AAA}(AAA(5))

```

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [May 22, 2020, 9:59pm UTC](https://discourse.julialang.org/t/can-this-be-done-with-an-outer-constructor/39955/3 "2020-05-22T21:59:12Z")

</div>

Thanks,

I was hoping I would not have to do that, but I guess that it has to do with how the default constructor is defined.
