Idea: invert() or toggle() function for boolean type?

Haskell does the sane (imo -- =P) thing of defining (^) :: (Num a, Integral b) => a -> b -> a as the power function. (this further demonstrates that ^= is not so readable... is it raising the LHS by a power or bitwise...?)

But x.fetch_add(1, ...) returns the value before adding 1, like C's post-increment x++.

1 Like

…and post-increment is often forbidden in C++ style guides because of its confusing nature and bizarre copying semantics.

I really don’t want ++ in Rust.

2 Likes

what about <-x++ (for post-increment) and x++-> (for pre-increment)? i.e. the arrow indicates when the output happens. and x++ itself is an ().

<-x++;
x += 1;

You win one character, and lose infinity readability. Symbolic inc/dec operators carry a lot of bad historical baggage for epsilon benefit.

let foo: i32 = x++; // error: expected i32, got ()
<-x++; // ok, but unnecessary
x++; // ok
let foo: i32 = <-x++; // ok
let foo: i32 = x++->; // ok

Please create a new thread if you want to talk about increments, this thread is about adding invert/toggle to boolean.

11 Likes

It’s not immediately clear that .toggle() or .invert() modifies self. From the first glance, I would assume that it just returns the inverse value without modifying the original one.

5 Likes

Interesting. I confess that had never occurred to me. However, it doesn't fit the pattern I want, because it returns ().

Ultimately, I usually decide that it's better off that I can't write the thing that I want, and just use two or three lines. It just...feels inelegant. But it's more obvious.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.