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


#22

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…?)


#23

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


#24

…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.


#25

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 ().


#26
<-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.


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

#28

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


#29

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.


#30

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


#31

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.