Open certik opened 1 month ago
This would be amazing!
I wonder if it could already be sort of standard-conforming, just by using the normal character kind syntax we already have? So, the compiler just has another character kind that means f strings are supported?
s = F_"{i} is an integer, {a:1.2f} is a rounded real and '{text}' is text"
This would be amazing!
I wonder if it could already be sort of standard-conforming, just by using the normal character kind syntax we already have? So, the compiler just has another character kind that means f strings are supported?
s = F_"{i} is an integer, {a:1.2f} is a rounded real and '{text}' is text"
I think that you're missing the hard part of the implementation in a compiler, which is the need to detect, collect, and resolve the variable designators (or worse, arbitrary expressions) from that character literal and pass them with their associated formats to the runtime I/O support library. This is not something that can be pre-packaged like a NAMELIST
-- those are limited to whole variables on output and only require a variant of list-directed editing.
And the proposed syntax that looks like a kind specifier is not a kind specifier, and would actually conflict with the need to specify a real character kind specifier for formatting to anything other than default-kind character.
@klausler we implemented parsing of a subset of f-strings in LPython, and the hard part is indeed arbitrary expressions inside the string. However, from personal experience, even just allowing variables is extremely useful. We can also limit f-strings to just compile-time strings, I never used them with runtime strings (not even sure that's allowed in Python). Regarding the implementation (which we have not done yet), one possibility is to do it at compile time, the other possibility is to convert f-strings to whatever formatting you already support in the compiler, and pass it together with the variables to runtime.
This issue is for implementing f-strings, inspired by Python: https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals
An example how they could work:
Previous discussions: