# Can I customize the accumulator type and result type for my types separately?

**URL:** <https://discourse.julialang.org/t/can-i-customize-the-accumulator-type-and-result-type-for-my-types-separately/43601>\
**Category:** New to Julia\
**Created:** [July 24, 2020, 3:19am UTC](https://discourse.julialang.org/t/can-i-customize-the-accumulator-type-and-result-type-for-my-types-separately/43601 "2020-07-24T03:19:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [July 24, 2020, 3:19am UTC](https://discourse.julialang.org/t/can-i-customize-the-accumulator-type-and-result-type-for-my-types-separately/43601/1 "2020-07-24T03:19:35Z")

</div>

In some cases of reduction operations, it may be useful to use special intermediate representations for speed and accuracy. A typical example is computing the sum of fixed-point numbers within the form of an integer accumulation.

We can customize `Base.reduce_empty` / `Base.reduce_first`. However, they also affect the final result type, and there seems not to be something like `Base.reduce_result` / `Base.reduce_final`.

Of course it’s possible to overload `sum`, `reduce`, etc, but I think that’s much more problematic than overloading `Base.reduce_empty` etc.

Any thoughts?

---

<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:** [July 24, 2020, 6:21am UTC](https://discourse.julialang.org/t/can-i-customize-the-accumulator-type-and-result-type-for-my-types-separately/43601/2 "2020-07-24T06:21:19Z")

</div>

Just convert the final result type to whatever you need?

---

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [July 24, 2020, 6:32am UTC](https://discourse.julialang.org/t/can-i-customize-the-accumulator-type-and-result-type-for-my-types-separately/43601/3 "2020-07-24T06:32:20Z")

</div>

That’s right. However I expect the customizations in packages, e.g. FixedPointNumbers.jl.

---

<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:** [July 24, 2020, 9:17am UTC](https://discourse.julialang.org/t/can-i-customize-the-accumulator-type-and-result-type-for-my-types-separately/43601/4 "2020-07-24T09:17:23Z")

</div>

One could imagine a framework for `reduce(op, itr)` where all these (first and empty element transformations, widening to an intermediate representation than narrownig, etc) would be customizable, but generally those methods would need to dispatch on `op` and `eltype(itr)`, at which point one may just name the operation (eg `op === +` as `sum`) and implement custom methods for that.

That said, a package could be a good place to experiment with this.
