# Minor style question: promotion inside constructor and new

**URL:** <https://discourse.julialang.org/t/minor-style-question-promotion-inside-constructor-and-new/33615>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 21, 2020, 11:21am UTC](https://discourse.julialang.org/t/minor-style-question-promotion-inside-constructor-and-new/33615 "2020-01-21T11:21:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 21, 2020, 11:21am UTC](https://discourse.julialang.org/t/minor-style-question-promotion-inside-constructor-and-new/33615/1 "2020-01-21T11:21:32Z")

</div>

This is a minor style question: I have a solution but it is a bit convoluted, and I wonder how others do it.

Suppose I want to cache something precomputed in a `struct` in the constructor (this is the only constructor, an inner one, and ensures that all instances are valid). At the same time, I would like to promote all fields to be the same type (to avoid unnecessary type proliferation).

MWE:

```julia
struct Example{T}
    a::T
    b::T
    c::T # a/b, saved
    function Example(a::T, b::T) where {T}
        c = a / b
        args = promote(a, b, c) # how to do this
        new{typeof(first(args))}(args...) # in a simpler way
    end
end

Example(1, 2)

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 21, 2020, 11:28am UTC](https://discourse.julialang.org/t/minor-style-question-promotion-inside-constructor-and-new/33615/2 "2020-01-21T11:28:35Z")

</div>

Instead of `typeof(first(promote(a,b,c)))` you could use `promote_type(T,typeof(c))`.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 21, 2020, 11:34am UTC](https://discourse.julialang.org/t/minor-style-question-promotion-inside-constructor-and-new/33615/3 "2020-01-21T11:34:04Z")

</div>

But then I would still need to `promote(a, b, c)` for the `new`.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [January 21, 2020, 12:21pm UTC](https://discourse.julialang.org/t/minor-style-question-promotion-inside-constructor-and-new/33615/4 "2020-01-21T12:21:03Z")

</div>

> [@Tamas\_Papp](#):
>
> But then I would still need to `promote(a, b, c)` for the `new` .

Actually no. They will be converted by default, so

```julia
struct Example{T}
           a::T
           b::T
           c::T # a/b, saved
           function Example(a::T, b::T) where {T}
               c = a / b
               new{promote_type(T,typeof(c))}(a,b,c)
           end
       end

```

should work.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 21, 2020, 12:31pm UTC](https://discourse.julialang.org/t/minor-style-question-promotion-inside-constructor-and-new/33615/5 "2020-01-21T12:31:03Z")

</div>

Indeed it does. I [missed it in the docs](https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#When-is-convert-called?-1). Thanks!
