RFC: result type calculations for arithmetic

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 and StaticArrays.arithmetic_closure are typical.

These calculations use various heuristics, which occasionally lead to bugs (eg this one), 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

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 :wink:).

1 Like

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.

1 Like

Another example in matrix product:

1 Like

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

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

julia> result_group(+,Int,Rational{Int})

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

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