-
Notifications
You must be signed in to change notification settings - Fork 311
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
Numeric format strings silently ignoring unsupported values or producing wrong results #4046
Comments
I didn't know Fable was doing that in some cases, but I found this example: TimeSpan.Zero.ToString("g") |> ignore Will make Fable compilation fails with this error:
@kerams Is it what you are referring too? |
I think so. Guess it's not a warning but an error. |
The warning one that I know about is something like if you pass a CultureInfo parameter Fable says, that it will just be ignored. I am just discovering the error one today thanks to you. |
I am looking at fixing this issue and I am able to fix the first 2 issues. Looking more into these issues, I have the feeling that we are having a different behavior / expectation depending on what API we are calling. If we call But if we call Finally, if we call I think it would be nice to coming up with a convention and try to apply it everywhere. To do so we need to decide what is the balance we want?
|
For reference, while we wait for a decision to be taken. To generate an error if no | "ToString", [ ExprTypeAs(String, _) ] ->
$"%s{i.DeclaringEntityFullName}.ToString with one argument is not supported, because it depends on local culture, please add CultureInfo.InvariantCulture"
|> addError com ctx.InlinePath r
None
| "ToString", [ ExprTypeAs(String, format); _culture ] ->
let format = emitExpr r String [ format ] "'{0:' + $0 + '}'"
Helper.LibCall(
com,
"String",
"format",
t,
[ format; thisArg.Value ],
[ format.Type; thisArg.Value.Type ],
?loc = r
)
|> Some To be applied here: Fable/src/Fable.Transforms/Replacements.fs Lines 2499 to 2511 in 8273959
|
@MangelMaxime Considering our limited resources, IMO all we can realistically shoot for is supporting |
Does Fable respect |
No it doesn't but this is perhaps possible to support it. We can probably retrieve the value of
I fully agree with that. Which leads me to think that things like:
should not be generated by Fable. We should allow user to write |
Just want to add that if a format is not supported, then generating an error independently of This is perhaps what you were was referring to @kerams. The discussion became messy because we have 2 topics happening at once in this issue I think. Sorry for that |
In .NET, |
Right for I will make sure to check that the documentation mention this. |
Looking back at this issue, I think we are trying to do too much. What I mean by that is the behavior in .NET when passing an invalid format is not to generate a compile time error but instead an exception at runtime. Furthermore trying to detect the format provided by the user is not 100% reliable for example: let myFormat (cond : bool) =
if cond then
"D"
else
"B"
(10).ToString(myFormat true) |> printfn "%A" In this code, trying to find |
Yes, we can only examine the Still, that covers the majority of the cases. My only point against a runtime error on unsupported formats was that a working F# code (on .NET) will be throwing a hard error on Fable, personally I would rather have some value back (and a compile-time warning) vs a hard runtime error from a code that works in .NET. But that's a personal preference, of course, so I understand other people might prefer the hard error. For the (minority of) cases where the format is not a string const and we cannot examine whether we can support it or not, we can just issue a general warning that we only support certain formats. This way we can have 100% "warning" coverage. As long as we have the warning, whether we throw an error on unsupported formats, or just the best value we can, is a matter of preference. |
In fsi, this prints 3 identical lines based on the current culture.
In Fable, the result is
x.ToString "#,#"
produces the correct string, but I'd appreciate a Fable warning saying that invariant culture will be used (I think I've seen something like that in other places).x.ToString "N0"
silently produces an incorrect string. An unsupported format error is warranted in my opinion.$"{x:N0}"
creates the compiled representation of the interpolation string, which is complete nonsense. An unsupported format error is warranted in my opinion.$"{1000:``#,#``} items"
results in%P(#,#) items
.The text was updated successfully, but these errors were encountered: