[RFC FS-1001 Discussion] String Interpolation redux #368
Replies: 91 comments
-
Use this link to get permalinks and add comments here about the RFC content. |
Beta Was this translation helpful? Give feedback.
-
resolved |
Beta Was this translation helpful? Give feedback.
-
One thing mentioned in the PR but not the RFC is interpolated verbatim strings. In C#, you need to do
|
Beta Was this translation helpful? Give feedback.
-
I suggest the ast construct
Additional considerations: If we allow side effects, then we must be careful about the evaluation order. |
Beta Was this translation helpful? Give feedback.
-
An interesting feature in the revised RFC is that, if the I can think of two approaches here:
let fmt = csprintf <| CultureInfo("es-ES", false)
let str = fmt $"Current time is: {DateTime.Now}"
let fmt = format (CultureInfo("es-ES", false)) >> sprintf
let str = fmt $"Current time is: {DateTime.Now}" |
Beta Was this translation helpful? Give feedback.
-
One detail that is not mentioned in the RFC: escaping. By my understanding, format strings as proposed have two special characters that signal the start of an interpolation, |
Beta Was this translation helpful? Give feedback.
-
@Tarmil, I've added a bit of information on escaping, in the RFC PR. Edit: |
Beta Was this translation helpful? Give feedback.
-
Nice, thanks! And good points, given these, doubling is better indeed. |
Beta Was this translation helpful? Give feedback.
-
I think the rationale behind these is that verbatim/multiline strings disable the |
Beta Was this translation helpful? Give feedback.
-
I do like this approach. Perhaps an instance member to avoid API pollution:
|
Beta Was this translation helpful? Give feedback.
-
Besides that we can choose. I think that I've clarified this in the RFC |
Beta Was this translation helpful? Give feedback.
-
I'll add this to the RFC via direct commit |
Beta Was this translation helpful? Give feedback.
-
Makes sense. Consistency to both sides are maintained. |
Beta Was this translation helpful? Give feedback.
-
Currently sprintf and derivatives use a reflection-based approach which is slow and AOT-unfriendly. This RFC extends sprintf syntax. Does this API entrench this problem or the API unrelated? If the sprintf logic were replaced with codegen in future, would having string interpolation increase the work? I'm not completely clear on whether efficient string formatting in F# would be based on an internal rewrite of sprintf or something completely new, perhaps based on https://github.com/fsharp/fslang-design/blob/master/RFCs/FS-1023-type-providers-generate-types-from-types.md . |
Beta Was this translation helpful? Give feedback.
-
If following the C# syntax, it might be more intuitive to mirror the C# behaviour as well i.e. allow similarly valid expressions. Perhaps offer some guidance in the docs as to intended best practices, but assume that devs will apply the same approach to code hygiene as in the rest of their codebase.
Yes - and trying to cater for these may lead to something like AngularJS templates, which can be a pain to deal with. Also, there's a risk of divergence or extra maintenance when implementing new language features. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to get a better understanding of your main concern and currently as I understand, I still think it's related: you have objection to (a single) string interpolation syntax to typecheck to multiple candidate types -- be it a |
Beta Was this translation helpful? Give feedback.
-
Yes but this not the main point. We can make syntax adjustments to disambiguate these if desired. The main point is that allowing interpolated string to give |
Beta Was this translation helpful? Give feedback.
-
I think the syntax adjustments are a bit noisy:
Plus it's redundant:
And again, back to the |
Beta Was this translation helpful? Give feedback.
-
I think addressed exactly these points in my earlier comment. The signature is exposed to users (just hover over |
Beta Was this translation helpful? Give feedback.
-
Understood. Thanks for your clarification.
|
Beta Was this translation helpful? Give feedback.
-
Clearly Using
Exactly the argument against using it. To do something simple it's better to use a simple and less powerful function than a complex and more powerful one. |
Beta Was this translation helpful? Give feedback.
-
So to simplify the discussion it's safe to assume we're not going to add these. let print = System.Console.Write: string->unit
let printn = System.Console.WriteLine: string->unit Why?
So to me, that pretty much means that this is supported: printfn $"I say again {x} is one and %0.2f{pi} is pi". That means interpolated strings support PrintfFormat. If we need to do added work to avoid beginners seeing
Taken to its logical conclusion I think this would mean not adding string interpolation at all. We have |
Beta Was this translation helpful? Give feedback.
-
If what is seen is a That would also allow let s = $"I say again {x} is one and %0.2f{pi} is pi"
printfn s Effectively |
Beta Was this translation helpful? Give feedback.
-
Just for my understanding, currently, this requires |
Beta Was this translation helpful? Give feedback.
-
That's right. Yes it would allow |
Beta Was this translation helpful? Give feedback.
-
There's no plan to change this as part of the RFC, I think it only complicates things to add a new form of overloading.
Here I was referring to the potential for simplifing the tooltip information given for Printf. But TBH the feedback that seeing |
Beta Was this translation helpful? Give feedback.
-
'{' is very common in json so its use to signal the start of an interpolation will be inconvenient when working with json snippets because interpolation can be used in a chuck of json all '{' characters need to be doubled up like so:
Aside for needing to double up the '{' the result is also difficult to scan visually. Is is also obviously not valid json defeating any tools such as analyzers that may want to interpret it as json. Of course any character chooses may be awkward in some domain ('<' would be a terrible choice in HTML). But perhaps it could be possible to specify the character used in some way. For example by putting it after the '$'. So |
Beta Was this translation helpful? Give feedback.
-
This is a reasonable observation. I'm not sure what the resolution should be though! For this RFC I guess we will just accept this limitation |
Beta Was this translation helpful? Give feedback.
-
Notes from the design meeting we had three weeks ago are here: dotnet/fsharp#8907 (comment) |
Beta Was this translation helpful? Give feedback.
-
I just went through the latest version of the rfc, and I noticed that I'm in favor of allowing it, though the current implementation disallows it. |
Beta Was this translation helpful? Give feedback.
-
This issue is used to track second round of discussions of F# RFC FS-1001 - "String interpolation"
Original discussion here: #6
Beta Was this translation helpful? Give feedback.
All reactions