Uh oh!
There was an error while loading. Please reload this page.
Allow non-inline literals to be used as printf format - #14670
Conversation
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
T-Gro
left a comment
There was a problem hiding this comment.
Left minor comments, happy and approving already.
Out of curiosity (and definitely not needed to do if this does not work):
Since we:
- Can derive literals from other literals using basic arithmetical operators (at least for numbers)
- Can have non-inline literals in
printfn
Could we:
- Support
printfnfor a "+"-appended sequence of string literals?
i.e. having string-literal snippets, and reusing them togeher by the "+" operator.
In order to have better string-reuse of repeatable snippets.
Yeah, that would be nice. However, string constants aren't folded outside of specific contexts (attributes, literal definitions, ...), so a more general solution would perhaps be better. I think I've seen a conditional compilation directive that disables constant folding of certain kind somewhere? System.Console.WriteLine ("sadasd"+"Av")compiles to Console.WriteLine(string.Concat("sadasd","Av"));whereas the C# compiler will concatenate the strings at compile time.
Interesting side-effect of these changes! While you can't concatenate the parts inline, you can still achieve reuse with a bit more verbosity let [<Literal>]coordinatesFormatSnippet="(%f, %f, %f)"let [<Literal>]playerPositionFormat="Player is at "+ coordinatesFormatSnippet
let [<Literal>]objectPositionFormat="Object is at "+ coordinatesFormatSnippet
letprintPlayerPosition p =
printfn playerPositionFormat p.X p.Y p.ZIt would also be cool to support interpolation in this context, but that'd be straying a bit too far from the original suggestion. let [<Literal>]coordinatesFormatSnippet="(%f, %f, %f)"letprintPlayerPosition p =
printfn $"Player is at {coordinatesFormatSnippet}" p.X p.Y p.Z |
T-Gro
commented
Feb 2, 2023
The reuse example is already good, I like it. I think the next level to allow this inline would be a separate issue (not sure if this needs a lang suggestion or not) to "Treat inline + concatenation of any string literals as a string literal as well" , doing the concat already at the compiler. It does have downsides though - will increase binary codesize for keeping that combined string in, in exchange for 1 less allocation at runtime. |
kerams
commented
Feb 11, 2023
Found a bug here, please don't merge. |
kerams
commented
Feb 12, 2023
Ready now. |
Uh oh!
There was an error while loading. Please reload this page.
Implements fsharp/fslang-suggestions#638, also extending it to IL constants, because why not.
I wanted to implement this as a type-directed conversion, but I wasn't sure whether I'd have to duplicate the logic from
TcFormatStringExprin MethodCalls.fs, because CheckExpressions is not available there. Since inline literal strings don't use this mechanism either, I chose the straightforward approach.