# \`convert\` vs constructor

**URL:** <https://discourse.julialang.org/t/convert-vs-constructor/61735>\
**Category:** General Usage\
**Created:** [May 24, 2021, 5:42pm UTC](https://discourse.julialang.org/t/convert-vs-constructor/61735 "2021-05-24T17:42:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianguenter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brianguenter/32/29519_2.png) [@brianguenter](https://discourse.julialang.org/u/brianguenter)\
**Post date:** [May 24, 2021, 5:42pm UTC](https://discourse.julialang.org/t/convert-vs-constructor/61735/1 "2021-05-24T17:42:17Z")

</div>

Which is considered better Julia style, this:

```julia
f(a::T) where{T<:Number} = T(2)

```

or this:

```julia
f(a::T) where{T<:Number} = convert(T,2)

```

Or are they equivalent in practice?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 24, 2021, 6:28pm UTC](https://discourse.julialang.org/t/convert-vs-constructor/61735/2 "2021-05-24T18:28:01Z")

</div>

They are often inequivalent. See the [manual](https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Conversion-vs.-Construction).

---

<div class="post-metadata">

**Author:** ![brianguenter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brianguenter/32/29519_2.png) [@brianguenter](https://discourse.julialang.org/u/brianguenter)\
**Post date:** [May 24, 2021, 7:02pm UTC](https://discourse.julialang.org/t/convert-vs-constructor/61735/3 "2021-05-24T19:02:09Z")

</div>

Sorry, I should have been more explicit in my question. I was asking for the particular case of Number types, not the more general case.

I believe this function is defined:

```julia
convert(::Type{T}, x::Number) where {T<:Number} = T(x)

```

which seems to imply that convert() and T() would be equivalent because convert reduces to calls to T().

Is this incorrect?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 24, 2021, 7:21pm UTC](https://discourse.julialang.org/t/convert-vs-constructor/61735/4 "2021-05-24T19:21:36Z")

</div>

It is entirely appropriate to define a conversion method `Base.convert`,  
to convert into a type T as a call to the constructor for the type T.  
Doing so suggests that there is no special handling (outside of that which may be part of the constructor’s own internal logic) required for a correct conversion from (here, `Number`) to `T`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 24, 2021, 7:28pm UTC](https://discourse.julialang.org/t/convert-vs-constructor/61735/5 "2021-05-24T19:28:32Z")

</div>

> [@brianguenter](#):
>
> I was asking for the particular case of Number types, not the more general case.

For `Number` types, I would typically just use `T(x)` since it is shorter than `convert(T, x)` and there should be [no difference in behavior](https://github.com/JuliaLang/julia/blob/0d975786083c6125b75df2b7ecb24c31a9d44fdd/base/number.jl#L7).
