# Avoid ambiguities with individual number element (identity)

**URL:** <https://discourse.julialang.org/t/avoid-ambiguities-with-individual-number-element-identity/62465>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 6, 2021, 1:16pm UTC](https://discourse.julialang.org/t/avoid-ambiguities-with-individual-number-element-identity/62465 "2021-06-06T13:16:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [June 6, 2021, 1:16pm UTC](https://discourse.julialang.org/t/avoid-ambiguities-with-individual-number-element-identity/62465/1 "2021-06-06T13:16:28Z")

</div>

I have a question concerning the overloading of operators for an own type

In our package on manifolds, we also cover Lie groups. These have an identity element (see [Manifolds.jl/group.jl at 7e90c455804311f1220bcb52878fa5abf86e0abb · JuliaManifolds/Manifolds.jl · GitHub](https://github.com/JuliaManifolds/Manifolds.jl/blob/7e90c455804311f1220bcb52878fa5abf86e0abb/src/groups/group.jl#L153-L156)) where we parametrise it by the group under consideration.

Usually we do not type our group elements, since we want to be open for other types. We usually think of matrices representing a point on the manifold, but other types are welcome, too.

Now when overloading operators (e.g. `isapprox`, but more prominently `+`,`-` or `*`,`/` - depending on the group action) we have definitions like (see [Manifolds.jl/group.jl at 7e90c455804311f1220bcb52878fa5abf86e0abb · JuliaManifolds/Manifolds.jl · GitHub](https://github.com/JuliaManifolds/Manifolds.jl/blob/7e90c455804311f1220bcb52878fa5abf86e0abb/src/groups/group.jl#L938-L948)).

```julia
Base.:+(e::Identity{G}) where {G<:AdditionGroup} = e
Base.:+(p::Identity{G}, ::Identity{G}) where {G<:AdditionGroup} = p
Base.:+(::Identity{G}, p) where {G<:AdditionGroup} = p
Base.:+(p, ::Identity{G}) where {G<:AdditionGroup} = p
Base.:+(e::E, ::E) where {G<:AdditionGroup,E<:Identity{G}} = e

```

1. How can I avoid ambiguities with other packages that also define a + this way, for example if we would like to use ChainRules ([https://github.com/JuliaDiff/ChainRulesCore.jl/blob/22c7bc368354428f0b62c350f1301e24e5644922/src/differential\_arithmetic.jl#L137-L143](https://github.com/JuliaDiff/ChainRulesCore.jl/blob/22c7bc368354428f0b62c350f1301e24e5644922/src/differential_arithmetic.jl#L137-L143))?  
I am not sure what the implications of this ambiguity (so whether it is important to resolve this) are and how often these appear
2. One possible ambiguity might appear if one `+`es two identities from different groups. This is not a valid operation. Would it be reasonable to provide a proper error message instead of the (very vague) Ambiguous error one would get?

Is there some coding style / good practice for these?

---

<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:** [June 7, 2021, 6:35am UTC](https://discourse.julialang.org/t/avoid-ambiguities-with-individual-number-element-identity/62465/2 "2021-06-07T06:35:48Z")

</div>

1. Just define the methods listed in the ambiguity warning. Metaprogramming is frequently used for code like this (look for `for ... @eval` evamples).
2. I would recommend a custom error hint, see `Base.Experimental.register_error_hint`.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [June 7, 2021, 6:50am UTC](https://discourse.julialang.org/t/avoid-ambiguities-with-individual-number-element-identity/62465/3 "2021-06-07T06:50:15Z")

</div>

The package [InitialValues](https://github.com/JuliaFolds/InitialValues.jl) also defines custom identity elements. From what I understand they use [Aqua](https://github.com/JuliaTesting/Aqua.jl) to test that there are no ambiguities. Maybe that could be useful in your case as well.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [June 7, 2021, 7:34am UTC](https://discourse.julialang.org/t/avoid-ambiguities-with-individual-number-element-identity/62465/4 "2021-06-07T07:34:57Z")

</div>

I’m a bit surprised that you decided to introduce an identity _type_ for your groups. Wouldn’t it be easier if you instead relied on the `one(x)` function to provide the identity element for your groups? If so, you wouldn’t have to define your own addition methods and all the problems in your original post disappear.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [June 7, 2021, 8:05am UTC](https://discourse.julialang.org/t/avoid-ambiguities-with-individual-number-element-identity/62465/5 "2021-06-07T08:05:36Z")

</div>

I thought about that, too; I will check what the effects will be then.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [June 7, 2021, 1:26pm UTC](https://discourse.julialang.org/t/avoid-ambiguities-with-individual-number-element-identity/62465/6 "2021-06-07T13:26:01Z")

</div>

This seems to be the correct solution, that will then be our long term goal; thanks for pointing this out. We still have to check where we actually need this type currently (i.e. where we really dispatch on what would be without that the value).
