There is an approved-in-principle proposal to extend the existing printf functionality in the F# language design with string interpolation. To discuss this design please us design discussion thread.
- Approved in principle
- Details: under discussion
- Implementation: Proof of concept submitted
Proposed syntax: "%(embedded expression)"
Initial implementation prototype has been submitted. Prototype accepts arbitrary F# expression as embedded expression. In prototype source string literal is split into chunks containing text and embedded expressions. Then chunks are joined using String.Concat.
Initial string literal:
"%d%(foo)%d%(bar.bar)"
After splitting:
Text("%d"); Expression(foo); Text(%d); Expression(bar.bar)
Final result
String.Concat([| "%d"; box foo; "%d"; box (bar.baz) |])
- Is general idea of implementing this feature entirely on semantic level is acceptable?
- Should embedded expressions be restricted to just identifiers\dotted names or we should allow full set of F# expressions?
- Under the hood String.Concat uses ToString to obtain string representation of the object (which is equivalent to "%O" format specifier in printf). This option is definitely not the best one for F# types like records\discriminated unions that are printed far more nicely with "%A". However for primitive types always using "%A" seems to be an overkill. Should we always prefer one way of printing things (and if yes - which one) or printing strategy should vary from type to type.
- Should we provide ways to specify width\precision\alignment similar to what printf is doing today? If yes - what modification should be made to the proposed syntax?
TBD