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
[Breaking] Warn unadorned string interpolation when the interpoland implements IFormattable / is generic #1306
Comments
String interpolation and culture sensitivity lead to tests failing depending on culture #10030 |
Actually we can exempt integers since they seem to format the same by default whether current culture or invariant culture. If they use .NET formatting specifiers then they would be looking for culture specific formatting - we can ignore them. |
The detection doesn't seem pristine here. It is an attempt to warn on culture-dependence. Something being F# string interpolation is not responsible for the culture-dependent behaviour here - objects implementing There is already a way to switch off the pitfall described: culture-invariant mode. |
@charlesroddie F#'s |
A quick test. Is this function pure?
Looks like the output purely depends on its inputs. Right?
sum 1 2
gives1 + 2 = 3
.sum 1.1 1.1
gives1.1 + 1.1 = 2.2
. Right?What a pitfall - there is action at a distance! This function is actually impure. But if we used the format specifiers:
We shouldn't create these pitfalls for newcomers to F#.
I propose we turn the warning against unadorned string interpolation on if the interpoland implements IFormattable or is generic.
The warning message would instruct the user to take action using one of the code fixes:
Invariants would come first since that is what F# code usually assume.
With #897 implemented (say
=
for equal everywhere aka "culture invariance" and#
for not equal everywhere aka "culture aware"), the new code after codefix would either look likeor
which are equivalent to
or
respectively, with one difference: the version without
O
is subject to FS3579 while the version withO
is not.The implementation can just change
.ToString()
to F#string
function which does this properly.This would only apply to types implementing IFormattable or generic types. Known non-IFormattable types like strings, booleans, DUs and records would be unaffected.
We can also warn for
%O
though it's not as important as the unadorned string interpolation.After 3 years (same timeframe as .NET LTS support), we may change the default to culture invariant and remove this warning. (This is what makes this suggestion not purely a tooling suggestion)
The existing way of approaching this problem in F# is either to look out for such mistakes manually or enforce usage of % formatting everywhere.
Pros and Cons
The advantages of making this adjustment to F# are
The disadvantage of making this adjustment to F# is that this is a breaking change for those who turn WarnAsError on.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): M
Related suggestions:
Affidavit (please submit!)
Please tick these items by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: