# RFC: result type calculations for arithmetic

**URL:** <https://discourse.julialang.org/t/rfc-result-type-calculations-for-arithmetic/20330>\
**Category:** Internals & Design\
**Tags:** proposal, announcement\
**Created:** [January 31, 2019, 6:43pm UTC](https://discourse.julialang.org/t/rfc-result-type-calculations-for-arithmetic/20330 "2019-01-31T18:43:23Z")\
**Posts on this page:** 6\
**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 31, 2019, 6:43pm UTC](https://discourse.julialang.org/t/rfc-result-type-calculations-for-arithmetic/20330/1 "2019-01-31T18:43:23Z")

</div>

A lot of Julia code, including `Base`, standard libraries, and packages, includes some helper function to calculate the resulting container type from various inputs, when combined with `+`, `-`, `*` and `/`.

Examples are too many to list here, eg [`LinearAlgebra.lutype`](https://github.com/JuliaLang/julia/blob/0b0a394b3e7741d38f00dbb29895b6ba6a7d0767/stdlib/LinearAlgebra/src/lu.jl#L145) and [`StaticArrays.arithmetic_closure`](https://github.com/JuliaArrays/StaticArrays.jl/blob/786b6f006bd9fcbf7445e6bef018a666f88a44da/src/arraymath.jl#L165) are typical.

These calculations use various heuristics, which occasionally lead to bugs (eg [this one](https://github.com/JuliaArrays/StaticArrays.jl/issues/487)), especially when various subtypes of `<:Real` are considered, eg `ForwardDiff.Dual`.

I thought it would be great to arrive at a single, canonical, well-tested implementation that packages could rely on. I wrote up something simple

> **[GitHub - tpapp/AlgebraResultTypes.jl: result type calculations for algebraic...](https://github.com/tpapp/AlgebraResultTypes.jl)**
>
> result type calculations for algebraic operations. Contribute to tpapp/AlgebraResultTypes.jl development by creating an account on GitHub.

to get the discussion started. I don’t insist on the implementation being in this package, it is just a way to get it tested in self-contained way.

Comments and suggestions would be appreciated (including bikeshedding names, but ideally conceptual issues. but yes, also bikeshedding names 😉).

---

<div class="post-metadata">

**Author:** ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)\
**Post date:** [January 31, 2019, 6:55pm UTC](https://discourse.julialang.org/t/rfc-result-type-calculations-for-arithmetic/20330/2 "2019-01-31T18:55:26Z")

</div>

One thing to consider is that there are algorithms where you want the return type but you only do computations over a ring and not a field, i.e., there is no division (or it promotes to a type you don’t want). An example of this would be polynomials.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [January 31, 2019, 9:57pm UTC](https://discourse.julialang.org/t/rfc-result-type-calculations-for-arithmetic/20330/3 "2019-01-31T21:57:01Z")

</div>

Another example in matrix product:

> <https://github.com/JuliaLang/julia/blob/c71b225e5ecc1acacffd050c7a7ac992bfe9d93a/stdlib/LinearAlgebra/src/matmul.jl#L5-L5>

> <https://github.com/JuliaLang/julia/blob/c71b225e5ecc1acacffd050c7a7ac992bfe9d93a/stdlib/LinearAlgebra/src/matmul.jl#L48-L51>

---

<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:** [February 1, 2019, 9:59am UTC](https://discourse.julialang.org/t/rfc-result-type-calculations-for-arithmetic/20330/4 "2019-02-01T09:59:37Z")

</div>

Thanks for the suggestion, I added a `result_ring` that does this.

---

<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:** [February 1, 2019, 4:40pm UTC](https://discourse.julialang.org/t/rfc-result-type-calculations-for-arithmetic/20330/5 "2019-02-01T16:40:34Z")

</div>

Made a pull request with a new method `result_group` for checking closure of group operations

```nohighlight
julia> result_group(+,Int,Rational{Int})
Float64

```

This one is a bit strange, since here for example, `inv(one(Int))` produces a `Float64`…

---

<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:** [February 4, 2019, 2:39pm UTC](https://discourse.julialang.org/t/rfc-result-type-calculations-for-arithmetic/20330/6 "2019-02-04T14:39:25Z")

</div>

Thanks for all the suggestion. Package (after name change) is now registered, with better docs.

> **[GitHub - tpapp/AlgebraResultTypes.jl: result type calculations for algebraic...](https://github.com/tpapp/AlgebraResultTypes.jl)**
>
> result type calculations for algebraic operations. Contribute to tpapp/AlgebraResultTypes.jl development by creating an account on GitHub.
