-
Notifications
You must be signed in to change notification settings - Fork 16
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
Implement f-strings #336
Comments
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 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:
The text was updated successfully, but these errors were encountered: