Stream: t-lang

Topic: ~ as bitwise complement?


Josh Triplett (Apr 27 2020 at 22:17, on Zulip):

Now (Rust 1.0 and later) that ~ is no longer a sigil for a pointer type, does it seem potentially feasible to make ~ a bitwise complement operator?

Josh Triplett (Apr 27 2020 at 22:17, on Zulip):

So that ~0 and !0 would mean the same thing, rather than forcing users to spell bitwise complement using the latter?

scottmcm (Apr 27 2020 at 22:18, on Zulip):

It's not obvious to me that having both here is worth it

Josh Triplett (Apr 27 2020 at 22:19, on Zulip):

It's certainly a minor but non-zero "surface area" increase. On the flipside, ! as bitwise complement spends some of Rust's "weirdness budget".

scottmcm (Apr 27 2020 at 22:19, on Zulip):

This is one of those places where we have the structural-fix on the error and you just apply it and done

Josh Triplett (Apr 27 2020 at 22:19, on Zulip):

scottmcm said:

This is one of those places where we have the structural-fix on the error and you just apply it and done

I'm thinking more about reading code.

Josh Triplett (Apr 27 2020 at 22:20, on Zulip):

When I'm reading low-level code, ! makes me double-take.

scottmcm (Apr 27 2020 at 22:22, on Zulip):

Yeah, this is one of those "given a time machine sure" things

Josh Triplett (Apr 27 2020 at 22:22, on Zulip):

Oh, certainly. I'm more wondering if it would be sufficiently non-disruptive to be worth doing now.

Josh Triplett (Apr 27 2020 at 22:22, on Zulip):

Cost: both operators exist and mean the same thing on numbers, so there's a slight surface-area increase. Some churn as people who prefer ~ convert their code.

Josh Triplett (Apr 27 2020 at 22:23, on Zulip):

Benefit: less confused developers from other languages, easier to read code.

Josh Triplett (Apr 27 2020 at 22:23, on Zulip):

It's a papercut, but a notable one.

scottmcm (Apr 27 2020 at 22:23, on Zulip):

Feels like it could be a lose-lose as "if it's non-disruptive enough to be feasible it's not valuable enough to do"

Josh Triplett (Apr 27 2020 at 22:24, on Zulip):

I'm not sure I know what you mean by that.

Josh Triplett (Apr 27 2020 at 22:24, on Zulip):

I'm not suggesting a deprecation (at most, an opt-in lint with a rustfix).

scottmcm (Apr 27 2020 at 22:35, on Zulip):

I guess I'm weakly not a fan if it's just there's two ways to do the same thing. I might be more interested if there were a migration plan, or a new ops trait that enabled new scenarios, or something like that.

I also feel like having the distinction opens the door for people asking for !a to be able to write a != 0 (after an edition change) since !a would no longer be necessary on integers

Josh Triplett (Apr 27 2020 at 22:36, on Zulip):

I see. Yeah, I'm not suggesting that we repurpose ! on integers, or break it.

Josh Triplett (Apr 27 2020 at 22:37, on Zulip):

I get what you mean, though. If we just make it an alias, then ~ will also accept bool types.

Josh Triplett (Apr 27 2020 at 22:37, on Zulip):

And if we make it a distinct operator, then it'll be a little surprising that Not and Complement on integers both return bitwise complement.

Amanieu (Apr 27 2020 at 22:57, on Zulip):

I think the ship has sailed at this point. People have been writing Rust for 5 years and know what !a means, it would be pointless churn to add a new way of doing the same thing.

Josh Triplett (Apr 27 2020 at 23:02, on Zulip):

Current users have been writing Rust for 5 years. New users encounter Rust every day.

Josh Triplett (Apr 27 2020 at 23:02, on Zulip):

The latter would have one less unusual thing to discover and trip over.

Lokathor (Apr 28 2020 at 02:21, on Zulip):

but making !a into ~a means that !true is now written as ~true, which is a poor side effect

Lokathor (Apr 28 2020 at 02:22, on Zulip):

and preferring ~a as "normal" supposes that people have a C background that many Rust users don't even have.

kennytm (Apr 28 2020 at 07:23, on Zulip):

in terms of weirdness budget, just wanna note that Go uses ^1234 as the bitwise-NOT operator

Josh Triplett (Apr 28 2020 at 07:40, on Zulip):

Lokathor said:

but making !a into ~a means that !true is now written as ~true, which is a poor side effect

That wasn't what I was proposing. I was proposing to keep ! and continue using it for bool.

Josh Triplett (Apr 28 2020 at 07:53, on Zulip):

Lokathor said:

and preferring ~a as "normal" supposes that people have a C background that many Rust users don't even have.

I just did a quick language survey. C, Python, JavaScript, Java, Ruby, C#, Swift, POSIX shell, Julia, PHP, SQL.

Josh Triplett (Apr 28 2020 at 07:53, on Zulip):

All of those use ~ as bitwise complement.

Lokathor (Apr 28 2020 at 16:20, on Zulip):

Well, even if that's the case, !a for "logical negation" and separately ~a for "bitwise compliment" seems like it's burning two operator characters for not much gain, because the logical negation is the bitwise compliment of a 1 bit value, as we all obviously know since we've all used rust for years.

nikomatsakis (Apr 28 2020 at 17:10, on Zulip):

I don't really buy that, I think separating the two operations is fine.

nikomatsakis (Apr 28 2020 at 17:10, on Zulip):

But I tend to agree it's disruption without much value.

nikomatsakis (Apr 28 2020 at 17:10, on Zulip):

On the other hand, it's a pretty minor issue.

Lokathor (Apr 28 2020 at 21:44, on Zulip):

We get a very small number of characters to make operators with. That's a factually true aspect of the ascii character set, which is what people type rust with.

So we have to be careful with how we assign them meaning simply because we don't get too many meanings before turning into Haskell (which many people don't want, but I guess I don't personally mind).

nikomatsakis (Apr 28 2020 at 22:00, on Zulip):

nikomatsakis said:

I don't really buy that, I think separating the two operations is fine.

Just to elaborate on this: in C already you have !x as distinct from ~x, and the same is true in Java and numerous other languages. I don't think there would be much expectation that ~x applies to boolean values.

nikomatsakis (Apr 28 2020 at 22:01, on Zulip):

I think I was responding, in particular, to this statement

Lokathor (Apr 29 2020 at 00:03, on Zulip):

Now that I've been reminded of it, I do vaguely remember the syntax from Java, though I don't think I ever used it myself.

I mean it's certainly possible to just make both expressions mean the same thing, I guess my point is that you gain nearly nothing at all (new people get used to using ! for bit-negate fairly quickly) and it eats a potential operator that we could use on something in the future.

Such as, just to name one random idea based on the other recent zulip topics: having an operator for numbers that just means "losslessly convert this from the type it is to the type i need right now and don't make a big deal about it, or compile error if you can't".

Josh Triplett (Apr 29 2020 at 02:47, on Zulip):

@Lokathor So, an operator that means .into()?

Lokathor (Apr 29 2020 at 03:01, on Zulip):

Sure, just one idea that was at hand, i bet a lot of people would like operators depending on what they do.

mark-i-m (Apr 30 2020 at 02:44, on Zulip):

I'm on the fence here. I definitely had a very hard time figuring out that ! was both logical and bitwise NOT when I first started. Perhaps the documentation is better these days, but I wouldn't mind make the change over an edition boundary, personally.

varkor (Apr 30 2020 at 12:22, on Zulip):

I thought this was cute when I first came across it, but I don't remember how I did. Certainly now there's a diagnostic if you try to use ~ suggesting !, but that doesn't help people reading who are more familiar with ~. Adding an extra operator just for bitwise complement seems a little excessive, though.

Last update: Jun 05 2020 at 23:10UTC