I had a condition, and depending on the condition I either created a None or a Some(value). So I thought, wouldn’t it be nice if I could just call a function on the condition that returns a Some(value) if the condition is met, and a None otherwise. So I wrote up some magical function, to see if it would look weird.
I’d like to get some opinions on the readability. Will this create more confusion than it’s worth? Also, it’s not really enough for its own crate, so if there are other suggestions that go along the same lines, a more general crate collecting similar traits and functions might be the right way to go.
If we put the inverse of this in the standard library, we could implement BitOr<Fn()> and create an OrElse operator a la C++'s goes to operator (as in while (x --> 0) {}):
Fixed. That ambiguity is unfortunate (along with the fact that impl would conflict with rust’s BitOr<bool> for bool impl. However, the following iff then operator would work (playpen http://is.gd/YD74XN):
Sorry, but I have to disagree. The && operator is designed to do a short-circuit boolean AND. This works well because the types on both sides are booleans and the result also is. Now if you put Option into the mix, you'll have to answer what happens when someone puts an option to the left side of the &&? What if we have an option on both sides?
let o1 = Some(..);
let o2 = None;
let b1 = true;
let b2 = false;
b1 && b2; // evaluates b1, then b2, returns false: bool
b1 && o1; // evaluates b1, then o1, returns Some(..): Option
b2 && o1; // evaluates b2, returns None: Option
o1 && b2; // ?
o2 && b1; // ?
o1 && o2; // ???
(b1 && o2) && (o1 && b2); // !?
o1 && b1 && o2 && b2 && Some("This is no longer funny"); // don't you agree?
Either we'd make the right-hand side type the result type (which would at least be a clear-cut rule), which could still confuse the hell out of people, or we'd make having a left-hand-side Option illegal (by not implementing the logical operator for this case), which would probably still be a footgun.
Sorry, but I have to disagree. The && operator is
designed to do a short-circuit boolean AND. This works well because the
types on both sides are booleans and the result also is. Now if you put Option into the mix, you'll have to answer what happens when someone puts an option to the left side of the &&? What if we have an option on both sides?
Plenty of languages support short-circuit AND on non-booleans, eg. javascript - there's nothing particularly problematic about it. The simplest rule would be to take the type of the right-hand side as you say, and all that's required is that the RHS type has a "falsey" value, but you could equally well go the whole way and accept arbitrary types on both sides:
(a: A) && (b: B) where &A: Into<bool>, A: And<B>
=>
if a.into() { a.and(b) } else { a.falsey() }
Where And<B> has an associated type which determines the return type of both "a.and(b)" and "a.falsey()"
The degree to which you're willing to sabotage readability in pursuit of a trifling improvement in writing ergonomics is terrifying nay, horrifying.
Nothing but bool should have any sort of "truthiness". The alternative leads to painful situations like Python's old "midnight is False", because everyone and their dog feels compelled to come up with some kind of "falsy" value for every type.
Even the smallest scrap of readability should be conceded only for significant gains. Losing "only bool is logical" to avoid some method calls is nowhere near enough.
Please, no. Just no.
Off-topic aside
eg. javascript - there's nothing particularly problematic about it
I cannot find words to express how much I don't agree with you.
Thanks, DanielKeep. You wrote much more eloquently what I wanted to say. I only wanted to add that this leads to people being ‘clever’ instead of doing the clever thing and writing readable code.
I’m sure it wouldn’t have taken long for someone clever to figure out that ! matches all types, so he could write Perl: mybool || panic!(...).
I’d be tempted to just copy+paste the methods from Option, and drop the closure argument, treating bool as though it were Option<()>. That way, there aren’t any new method names to learn.
[quote=“kstep, post:15, topic:1729, full:true”]
I like it. But I’d favor a shorter name for to_option. Maybe to_opt? Or iff for that matter, as it was purposed here earlier.
[/quote]The abbreviation would be frowned upon. And this function doesn’t feel like if. Maybe it could just be option.
I would be confusing by the name “map” here, as map always maps a function over one or more elements. Here, the function gets no arguments. to_option or and_then would be much better.