-
Notifications
You must be signed in to change notification settings - Fork 21
bool::implies and bool::implied_by #188
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
Comments
My big question is why this set it worth having specifically. After all, there's no specific method for |
I think this operation is worth having because it has a well-known name, so writing |
I'm not sure if this is a good addition to the standard library. Someone might end up using |
The same argument also applies to having I look at it this way: either a programmer may (for some reason) write |
I don't think Ord on booleans is a good precedent. I have run into cases where it felt quite arbitrary.
For the former we could chose a more neutral name that just describes what it does rather than ascribing meaning. |
The former is already expressible by just doing |
I understand the truth table of implication, but stuff like |
The latter is less general and can be expressed via an extension trait. So the question is whether it's appropriate for the standard library. |
It can be expressed via an extension trait, but it probably won’t be expressed that way, since writing an extension trait for a couple of single-line methods is kind of cumbersome. |
I'm skeptical of "it has to be in the standard library or else nobody will ever write it" arguments, that's more a sign that its utility is quite low, rather than an argument in favor. At the very least if someone thinks it makes their code more readable they could write a free function or a macro inside their own crate. Is their prior art of crate authors trying to improve readability this way? |
It seems like people are writing such a trait / free function in their code: https://github.com/search?q=lang%3ARust+symbol%3Aimplies&type=code |
It seems like you are still on the old Github code search then that can't handle symbols, languages and co. yet. |
Ah yeah, enabling the preview worked. Interesting, I get pre-1.0 hits for rust, which means it was removed at some point. One library chose |
That instance was moved to an extension trait in rust-lang/rust#10007, and then removed in rust-lang/rust#12473. |
I don't think this is a very well known name. Most programmers will be able to read |
I agree with this, but I also think (as mentioned above) that an extension trait is a suitable answer to this. I don't think we should be lifting this terminology to std. Its negative effects are likely to eclipse its positive effects IMO. |
This really made me think. If the main argument against implies (well there's also the name) is that it seems uncommon and there's no reason to have it when other operators don't have a method... then what if we simply add all of them?
Then it's not arbitrary and I'm sure people would appreciate the others (NAND, NOR and co.) as well. Integers, floats and co. have lots of methods nowadays, so I don't see why we couldn't simply add the whole truth table to booleans (it's only 7 methods). That unfortunately doesn't solve the naming problem (but maybe if we add them all we could just go with the official names for the operators?). |
we could use method names that describe what operation they do in terms of
|
I am personally even less in favor of adding the full complement of methods here than I am of just adding something like |
We discussed this in the libs-api meeting. Overall we believe that this makes code less readable than if the condition was explicitly written out using basic boolean operations (#188 (comment)). If this was added to the standard library then it may encourage people to use it because it matches the truth table they are looking for, at the cost of readability. |
Uh oh!
There was an error while loading. Please reload this page.
Proposal
Problem statement
There’s no clean way to express boolean implication: you need to resort to something like
!a || b
.Motivation, use-cases
Happens kind of a lot, but it may be unclear whether
a.implies(b)
or!a || b
is more clear in each concrete use case.Some examples from rustc:
https://github.com/rust-lang/rust/blob/f63ccaf25f74151a5d8ce057904cd944074b01d2/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs#L451
https://github.com/rust-lang/rust/blob/f63ccaf25f74151a5d8ce057904cd944074b01d2/compiler/rustc_expand/src/config.rs#L444
https://github.com/rust-lang/rust/blob/f63ccaf25f74151a5d8ce057904cd944074b01d2/src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs#L44
Solution sketches
Links and related work
Some languages have implication function or operator, e.g. Racket.
Rust has
<=
, which works like boolean implication but looks like implication backwards, which is kinda confusing.What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
The text was updated successfully, but these errors were encountered: