Skip to content
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

Open
certik opened this issue Jul 23, 2024 · 3 comments
Open

Implement f-strings #336

certik opened this issue Jul 23, 2024 · 3 comments

Comments

@certik
Copy link
Member

certik commented Jul 23, 2024

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:

character(:), allocatable :: s, text
integer :: i
real :: a
i=1
a=1.123456789
text="this"
s = f"{i} is an integer, {a:1.2f} is a rounded real and '{text}' is text"
print *, s

Previous discussions:

@certik certik transferred this issue from j3-fortran/generics Jul 23, 2024
@jacobwilliams
Copy link

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"

@klausler
Copy link

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.

@certik
Copy link
Member Author

certik commented Jul 23, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants