-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature request: Compose
trait for strings and functions
#16541
Comments
Not sure if this would be a good place to bring this up, but a good, generic replacement for Haskell's Of note, this does work for functions of |
Personally I think this request is RFC-worthy and a function composition operator can be considered once unboxed closures are stabilized. (Though I am not sure if Rust needs a dedicated operator for that, as Haskell's “multi-arity” functions are all effectively unary, but Rust's are not.) I don't like unifying both string concatenation and function composition under the same I propose that we add two traits, |
@CloudiDust Strings are monoids under concatenation with identity ε; functions |
Adding a new operator sugar is absolutely something that needs to go through the RFC process. Here's the link: rfc process |
(closing as this needs to go through rfc process) |
@apoelstra, I see, and I now suggest that we use
I get the inspiration from the Wikipedia article on monoid, especially the section "Monoids in computer science". |
Highlight rustdoc Updated version of rust-lang/rust-analyzer#16340
internal: Revert rust-lang#16541 Closes rust-lang/rust-analyzer#16602
As @huonw mentions in #14482, mathematical convention is that addition is commutative. The stdlib already follows mathematical convention in some cases (for example,
Zero
requiresAdd
and is expected to be the additive identity; ditto forOne
andMul
).Currently everything in the stdlib which implements
Add
implementsadd
as a commutative operator, except for strings. Therefore I propose:Compose
trait with acompose
function that sugars to the++
operator.String
for concatenation, andFn
traits for composition (edit this actually doesn't make sense for functions that aren'tA->B
andB->C
, never mind). This replacesadd
forString
.Add
.This will help in writing generic code, since it is an (often unstated) assumption in many algorithms that
+
is a commutative operator. It'll also make analysis easier since programmers won't have to memorize that+
means something different in rust than it does everywhere else.It might also be worthwhile to add an
empty()
constructor toCompose
to make it a monoid trait. Though as reem points out on IRC,Compose+Default
is probably preferable as it is more explicit.Edit: Perhaps this should be on discourse, but it's a very minor (though breaking) change that cleans up the API and doesn't remove any functionality, and doesn't have any room for bikeshedding that I see. Sorry if I'm wrong on this point.
The text was updated successfully, but these errors were encountered: