Julia: Change `one` to `onemult`

Created on 1 Jun 2020  路  8Comments  路  Source: JuliaLang/julia

This is a persistent source of confusion (most recently #36099), and there are incorrect definitions of one scattered across the ecosystem. (We're still deprecating them in Colors-world.) The best answer is to @deprecate one onemult and simply not have a function called one.

Most helpful comment

What about adding the 2-arg identity(op, x)?

Edit
As you know, this comes from #16116.

All 8 comments

What about adding the 2-arg identity(op, x)?

Edit
As you know, this comes from #16116.

I think onemult will just change the FAQ to "Why can I use zero but not one? It's inconsistent!" :stuck_out_tongue:

What about adding the 2-arg identity(op, x)?

"Why is there oneunit but no zerounit?" :stuck_out_tongue:

This ship has probably already sailed, but I've often thought that zero and one should be instances of singleton types Zero and One, representing the typeless concepts of additive and multiplicative identity with fallbacks like +(::Zero, x) = x and *(::One, x) = x. Related discussions: #8092, #16116, #18367, #20561.

but I've often thought that zero and one should be instances of singleton types Zero and One

This would make a lot of functions type-unstable. e.g. sum(T[]) returns zero(T), so returning Zero() instead would make sum type-unstable (since the type of the result would differ for empty vs. non-empty arrays of the same type).

Of course if the type is known then one should return the zero element of that type. I鈥檓 thinking of cases where the type is not known.

But even if it were used in general, the return type would be Union{Zero,T}. I was under the impression such small unions are now reasonably performant.

Singleton zero is discussed in https://github.com/JuliaLang/julia/issues/34003. I also mentioned other identities there too.

I think it's better to focus on onemult in this issue.

How about deprecating to Base.reduce_empty(*, T) (which already exists) instead of onemult(T)? The only place where you really need this is as the return value of empty reductions.

In all other cases (that I can think of) you can either use true or oneunit.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Keno picture Keno  路  3Comments

dpsanders picture dpsanders  路  3Comments

i-apellaniz picture i-apellaniz  路  3Comments

felixrehren picture felixrehren  路  3Comments

ararslan picture ararslan  路  3Comments