# Conversion while constructing object

**URL:** <https://discourse.julialang.org/t/conversion-while-constructing-object/77401>\
**Category:** New to Julia\
**Created:** [March 4, 2022, 2:00pm UTC](https://discourse.julialang.org/t/conversion-while-constructing-object/77401 "2022-03-04T14:00:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![KDr2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdr2/32/2542_2.png) [@KDr2](https://discourse.julialang.org/u/KDr2)\
**Post date:** [March 4, 2022, 2:00pm UTC](https://discourse.julialang.org/t/conversion-while-constructing-object/77401/1 "2022-03-04T14:00:26Z")

</div>

Hi, does anyone know in the code below, why `#2` has `convert` called and how to avoid it?

```julia
struct S{T}
    i::T
end

mutable struct Box{T}
    i::T
end

function Base.convert(::Type{S{T1}}, d::S{T2}) where {T1, T2}
    println("Convert: ", T2, " --> ", T1)
    return S{T1}(zero(T1))
end

function Base.copy(box::Box{T}) where {T}
    println("Different Types? --> ", T, ", ", typeof(box.i))
    Box{T}(box.i)
end

# 1.
b = Box{Any}(S{Float64}(1.0)) # no conversoion
c = copy(b) # no conversion

# 2.
b = Box(S{Float64}(1.0)) # Conversion 
c = copy(b) # Conversion 

```

Thanks!

---

<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:** [March 4, 2022, 2:12pm UTC](https://discourse.julialang.org/t/conversion-while-constructing-object/77401/2 "2022-03-04T14:12:23Z")

</div>

It’s the default constructor that implements the conversion. If you don’t want any default constructors, define your own inner constructor.

[https://docs.julialang.org/en/v1/manual/constructors/](https://docs.julialang.org/en/v1/manual/constructors/)

---

<div class="post-metadata">

**Author:** ![KDr2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdr2/32/2542_2.png) [@KDr2](https://discourse.julialang.org/u/KDr2)\
**Post date:** [March 4, 2022, 2:31pm UTC](https://discourse.julialang.org/t/conversion-while-constructing-object/77401/3 "2022-03-04T14:31:31Z")

</div>

I defined my own constructors, and they didn’t change the behavior…

```julia
mutable struct Box{T}
    i::T
    Box(i::T) where T = new{T}(i)
    Box{T}(i) where T = new{T}(i)
end

```

---

<div class="post-metadata">

**Author:** ![KDr2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdr2/32/2542_2.png) [@KDr2](https://discourse.julialang.org/u/KDr2)\
**Post date:** [March 4, 2022, 2:40pm UTC](https://discourse.julialang.org/t/conversion-while-constructing-object/77401/4 "2022-03-04T14:40:57Z")

</div>

and `b.i = S{Float64}(1.0)` also calls `convert` while `b.i` and `S{Float64}(1.0)` have the same type `S{Float64}`.

Does converting `a::T` to type `T` make any sense?

---

<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:** [March 4, 2022, 2:59pm UTC](https://discourse.julialang.org/t/conversion-while-constructing-object/77401/5 "2022-03-04T14:59:46Z")

</div>

Yes, there’s a default `convert(::Type{T}, x::T) where {T} = x` method builtin, but your convert method is more specific. You could similarly add a `convert(::Type{S{T}}, x::S{T}) where {T} = x` to avoid this case.

I guess I typically don’t see that `convert` call because of the no-op default.

---

<div class="post-metadata">

**Author:** ![KDr2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdr2/32/2542_2.png) [@KDr2](https://discourse.julialang.org/u/KDr2)\
**Post date:** [March 4, 2022, 3:07pm UTC](https://discourse.julialang.org/t/conversion-while-constructing-object/77401/6 "2022-03-04T15:07:02Z")

</div>

Ahhh, that explains the situation, thank you.

But in my case `struct S` and its `convert` are from another package[1], and `Box` is my own type, so I should:

- either file an issue (or PR) to wait for the fixing
- or do a type-pirate to add a `convert` method myself

Is that right?

[1] : [https://github.com/JuliaStats/Distributions.jl/blob/master/src/univariate/continuous/inversegamma.jl#L48](https://github.com/JuliaStats/Distributions.jl/blob/master/src/univariate/continuous/inversegamma.jl#L48)
