Add an optional "+" prefix to numeric literals


#1

In some contexts, specifically adding a + in front of a numeric literals can enhance readability and given that -1.0 is a valid syntax, +1.0 should be also.


#2

My opinion: More complexity, bikeshedding needed, so I’m against it.


#3

Here’s the bikeshed you ordered :smile:

  1. Do we support unary-plus for general expressions: +expr;?

  2. Can it be allowed on non-numbers: +"string"?

  3. How about +BigInt::new("3")?

  4. Can unary-plus be overloaded? trait Pos { type Result; fn pos(self) -> Self::Result; }

  5. Or should it just be a sugar for “no-op”?

  6. Should Pos just be a marker trait if we don’t want +"string" to be valid?

  7. What should be the name of this trait, if it is needed?


#4

I’ve never seen an unary + in school-math, college-math or later math. I don’t see how it would be more readable. If you are worried about alignment, make an rfc for the style guide to add spaces to align numbers.


#5

I would vote for sugar for no-op. Just for “mathematical” consistency.


#6

frequently used for quantities that are sometimes positive and sometimes negative like charge

but can this create ambiguities in the future?


#7

I think the main reason for adding this is because all the other programming languages support it. The syntax would just be a + before a number where ever a - is allowed.

However, rust hasn’t been into the trend of “doing things because other people do them”, so I’m not sure we need this.


#8

A completely different idea for the use of a unary plus in a language…

Once upon a time I made a programming language named Reia:

http://reia-lang.org/

I eventually got quite emo about it after realizing that making a programming language is really hard and that the language I created really wasn’t the one I wanted to use.

Circa 2011 I wrote up my notes about what I’d like the next language I designed (probably not going to happen, thanks Rust!) to look like:

In it I proposed the otherwise useless unary + operator as bestowing a particular capability on its operand, specifically in the case of literals it made them mutable.

I’m not saying that’s a good idea for Rust, but before you consider making unary “+” a useless operation simply because that’s what all the other languages are doing, perhaps think about whether you could use it in a useful way.