The C# language (and other languages I’m sure) require suffixes at the end of numeric literals. These suffixes indicate the type of the literal. For example, 5m is a decimal, 5f is a floating point number.
My question is: are these suffixes really necessary, or is it possible to infer the type of a literal from its context?
For example, the code decimal d = 5.0 should infer that 5.0 is not a double, but a decimal. Does that kind of grammar cause problems?
That’s fine for simple cases like:
but it’s far better to be explicit so that you don’t have to worry about statements like:
Yes, I know that’s a contrived example but we don’t know the intent of the coder from just the type on the left, especially if it’s not being assigned to a variable at all (such as being passed as an argument to an overloaded function that can take one of int, float, double, decimal et al).