-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow simple arithmetic in number literals #539
Comments
I was surprised I couldn't use |
@rmunn Marking this approved in principle |
@rmunn It would be great if you could work with the community to submit an RFC and implementation in the F# 4.2 timeframe (whatever that turns out to be :) ) Thanks! |
I've had a lot on my plate but I haven't forgotten this. I hope to get an RFC submitted sometime within the next month. |
Would this feature carry over to attributes and other situations where we use literals; not only applying to
|
An additional case to what @jwosty has mentioned is converting literal expressions to enums: let [<Literal>] someValue = 1
[<AttributeWithSomeEnum(enum (someValue + 1))>]
do () It's possible to do this in C#, and I've stumbled upon this and had to introduce another named literal, which isn't good since the referenced literal may change without changing the extra literal: let [<Literal>] LayerSyntaxPlusOne = 3001 // HighlighterLayer.ADDITIONAL_SYNTAX + 1 |
Can someone write an RFC for this please? :D |
This could be a nice addition to F#, especially when working with Enums. F# 7.1 maybe? But we need a hero. |
Any RFC takers --> I'll gladly review 😄 |
This is done. |
Should probably move RFC to preview |
Does the implementation cover all literals (such as |
Both |
fsharp/fslang-design#724 could incidentally do with merging. |
I'm actually surprised that this hasn't been proposed before (or if it has, I wasn't able to turn it up with multiple searches). I propose we allow the following to compile:
In addition, I'd like to see other basic arithmetic operators supported, like
-
,*
,/
and%
(which might only be allowed for positive integers). Finally, it might also be nice to support bit-shift operators>>>
and<<<
, as well as bitwise operators like&&&
and|||
. E.g.,The existing way of approaching this problem in F# is to simply specify each of these values as a single number:
However, this runs the risk of the constant values, which are supposed to relate to each other, getting out of sync. It would be preferable to be able to change the
bitShift
constant to 5 and have themultiplier
andbitMask
constants automatically become 32 and 31 respectively. That would allow declaringbitShift
at the top of the source file, and declaringmultiplier
andbitMask
much closer to where they're used. With the current syntax requirements for literals, it's necessary to declaremultiplier
andbitMask
near the top along with thebitShift
value, so that if anyone changesbitShift
later, they'll remember to change the other two related constants at the same time.Pros and Cons
The advantages of making this adjustment to F# are being able to more clearly express the intent behind numeric literals that should be related to each other, and being able to change all of them with a single code change.
The disadvantages of making this adjustment to F# are that the compiler would have to keep track of previously evaluated literal names, so that it could use them in evaluating subsequent literals.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions: #129 might be related. That one was declined because there's a way to implement it with type providers; however, I can see no obvious way to implement this suggestion with type providers.
Affidavit (must be submitted)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: