Target Typed Number Literals #2929
Replies: 75 comments
-
var value = 0.1f + 0.1;// The second one is a float since it's being added to a float. This is a breaking change. |
Beta Was this translation helpful? Give feedback.
-
You're right. I've edited the OP. |
Beta Was this translation helpful? Give feedback.
-
So the only change is to allow |
Beta Was this translation helpful? Give feedback.
-
Yes, though it would most often be used as |
Beta Was this translation helpful? Give feedback.
-
For
void Method(float f);
void Method(double d); If you write At the moment, floating-point literals are nice and clear: if it ends with |
Beta Was this translation helpful? Give feedback.
-
True, but that's already a compiler error so it's hardly a big deal. It would just change from "can't implicitly convert literal" to "constant is out of range" which is more immediately useful. Currently you would see the former error, stick an f on it, and then see the latter instead of just getting the latter error upfront.
If I wrote that, the latter would 100% be what I intended it to do. I'm not specifically telling it to use doubles anywhere and it's faster without the cast. If you actually intend a double literal you can always use 0.1d, but that's far less likely if you are already working with two floats.
What new rules? The rule I'm proposing would address overload resolution in the same way as it addresses everything else: if it can't be a double, try a float (and the same for int -> long). In this case it can be a double, so it's a double. But the vast majority of systems I know of only deal with one type or the other. For me in Unity that's usually floats and I'd love to be able to use
I disagree. Which one do you think |
Beta Was this translation helpful? Give feedback.
-
@canton7
I can't see any reason why this should be different, but then again I don't know if this is worth the effort. |
Beta Was this translation helpful? Give feedback.
-
The thing is, that isn't necessarily true for everyone. I know that
See section 7.5.3.5 of the spec. |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt that's not quite the same as this proposal: If this were just a proposal to allow However, the proposal is about finding all double literals in an expression and implicitly turning them into float literals if the target type of the expression is a float (or could be a float, if a suitable method overload, etc, is found). |
Beta Was this translation helpful? Give feedback.
-
That seems obvious; you draw the line where it can't be done without loss of precision for the same reason you can't use
But how meaningful is that distinction in practice? The result is not much of a double since it is being assigned immediately to a float. My understanding is that the precision of internal calculations like that already varies based on the hardware so the exact values are anyone's guess until it actually gets assigned to your float so you can use the result, at which point it's obviously not a double. If you write
Not quite. I'm proposing implicitly turning them into floats if they can't be doubles, which seems like an important difference because it would only come into play if the old way fails. |
Beta Was this translation helpful? Give feedback.
-
In other words, |
Beta Was this translation helpful? Give feedback.
-
This will be very useful for decimals. decimal price = 9.99; // currently not allowed |
Beta Was this translation helpful? Give feedback.
-
Convert to float if it can't be a double and if sticking
|
Beta Was this translation helpful? Give feedback.
-
Note that |
Beta Was this translation helpful? Give feedback.
-
How about a simpler alternative: allowing an implicit conversion from a double literal to a float literal? That gets you almost all of the way there, and it's quite a bit simpler.
It differs from your suggestion in that To be more specific, if the implicit conversion from a double literal to a float is chosen, then it is compiled as if it was a float literal, with all of the same (albeit tweaked) errors for e.g. |
Beta Was this translation helpful? Give feedback.
-
Then feel free to ignore it. It was just me pointing out that we are extremely wary about code changing meaning. |
Beta Was this translation helpful? Give feedback.
-
Note: this discussion doesn't seem to really be adding anything new. I've stated my position, and the information i'm looking for in order to potentially change it. You're welcome to provide new info (or not). Up to you :) gitter.im/dotnet/csharplang is also a good plae to discus this stuff and work with people as sounding boards to weigh out ideas and proposals. You may have more luck there refining things. Good luck! :) |
Beta Was this translation helpful? Give feedback.
-
These two statements directly contradict each other. You made no mention of something being disallowed because it would change the meaning. |
Beta Was this translation helpful? Give feedback.
-
I literally did not say the first thing. You can see that right here:
See the bolded part. So you're stating there's a contradiction between something i said and something i did not say. |
Beta Was this translation helpful? Give feedback.
-
Again, this discussion is not proving fruitful at all. I've stated my major objections to this proposal, both at a high level, and against the particular details (as best as i can distill them). I welcome any further contributions toward resolving those particular areas. I also would be very willing to continue the discussion in a more productive venue (like gitter.im/dotnet/roslyn, or https://discord.gg/csharp). I think that would work out a lot better. Thanks, and i hope to hear from you :) |
Beta Was this translation helpful? Give feedback.
-
Yes. Therefore omitting the type might or might not change the meaning. Therefore your original statement that "you can omit the type when it would not change the meaning" can be added with " and you can also omit the type when it would change the meaning". Therefore your original statement means nothing more than "you can omit the type", which does not say anything about your wariness to allow code to change meaning. Which is a contradiction of your later statement that "It was just me pointing out that we are extremely wary about code changing meaning." |
Beta Was this translation helpful? Give feedback.
-
Again, you are responding to a statmenet i didn't make. You claimed i was stating that. And all i'm saying is: i didn't state that. Just because i didn't state it doesn't mean you then get to go make another claim about what i'm saying. All i actually claimed was:
the rest of tehse statements are ones of your own construction in response to statement that you claimed (incorrectly) that i said in the first place. |
Beta Was this translation helpful? Give feedback.
-
IN other words: Cyrus said: X That's it. I made a claim. That claim is factual and is what i was trying ot state. You've made several other statements based on things i didn't actually say (and then made ever increasing statements on top of htat). These are all strawmen built on each other. |
Beta Was this translation helpful? Give feedback.
-
Your original statement "you can omit the type when it would not change the meaning" tells me what I can do. Not what I can't do. And not what you are wary of allowing people to do. |
Beta Was this translation helpful? Give feedback.
-
Your claim is factual, but useless. It doesn't demonstrate the point you were trying to make. |
Beta Was this translation helpful? Give feedback.
-
Yes. That was the point. It's very much the intention on how 'var' was designed. |
Beta Was this translation helpful? Give feedback.
-
#2929 (comment) This is going in circles. And it's completely pointless and non helpful to the actual goal of pushing this proposal in some better direction. I literally do not understand what you think this is buying your case. However the offer still stands to right the ship and get back to actually addressing the problems as i see it with the proposal, ideally in a location more conducive to that sort of conversation. Cheers! |
Beta Was this translation helpful? Give feedback.
-
But according to your later statement, the point was somehow connected to you being wary of allowing the opposite of what you said. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
The discussion about strawmen and claims of what were said are not furthering the language feature proposal at all. For the purposes of putting this to bed: i retract 100% of every statement related to var. They have served only to distract you and get off of the valuable course of discussing this feature proposal. It benefits no one to continue on it. literally nothing fruitful is coming out of it. I cannot understand your insistence on keepign the discussion on it. So, if it makes you willing to stop talking about it i will simply state either of the following if it works for you:
Will that get us off of this and back onto the main topic again? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Along the same lines as target typed new, Switch Expressions, and Name Lookup, what about target typed literals:
In general terms, when I write
0.1
I don't usually care whether it's a float or double. It's a floating point value so it's going to be imprecise either way and any calculations it's used in can vary in precision depending on the hardware, so I already need to acknowledge the possibility of floating point error and inconsistencies. So I propose allowing0.1
to mean that I don't really care which type it uses; still default to double to avoid breaking old code, but if it won't compile then see if it can do so using the target type.In more technical terms, if an unmarked numeric literal (
0.1
rather than0.1f
or0.1d
) would not be valid as its default type (0.1
defaults todouble
), then you can treat it as if it had the suffix for the target type (or if it is an expression, then this applies to any numeric literals in that expression). So sincesomeFloat = 0.1
isn't allowed, treat it assomeFloat = 0.1f
instead. And sincesomeDecimal = 0.1
isn't allowed, treat it assomeDecimal = 0.1M
.Benefits
float
anddouble
without needing to add/remove thef
.float
anddouble
to understand why you need to dropf
bombs everywhere. Decimals can be read by normal people instead ofdecimal value = 0.1M
looking like it could be referring to Meters or Millions.float value = someFloat + 1
andbyte value = 1
.Beta Was this translation helpful? Give feedback.
All reactions