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

Support f-string formatting #2050

Open
Tracked by #1501
plajjan opened this issue Dec 28, 2024 · 4 comments
Open
Tracked by #1501

Support f-string formatting #2050

plajjan opened this issue Dec 28, 2024 · 4 comments
Labels
enhancement New feature or request syntax Related to the Acton language syntax / design

Comments

@plajjan
Copy link
Contributor

plajjan commented Dec 28, 2024

We should add support for using f-strings for formatting! It's the modern format and in fact so popular (which in this case I think is an indicator for really good & intuitive) that other languages have mimicked Python, like the C++ fmt library etc.

name = "world"
message = f"Hello {name}!"

# Big integers
big_num = 12345678901234567890
message = f"The value is {big_num}"  # Beyond i64 range

# Expressions
base = 2
exp = 10
result = f"{base}^{exp} = {base**exp}"  # "2^10 = 1024"
@plajjan plajjan added enhancement New feature or request syntax Related to the Acton language syntax / design labels Dec 28, 2024
@plajjan
Copy link
Contributor Author

plajjan commented Dec 28, 2024

@sydow WDYT? Is this relatively straight forward or a complex task?

@plajjan
Copy link
Contributor Author

plajjan commented Dec 28, 2024

I can't help but wonder if we should get rid of the current style, i.e.:

bla = "foo %d" % 123

it's just a carry-over from older Python, which in turn got it from C... but like f-strings just seem better in most ways, so let's get rid of ambiguity etc and just support a single format?

@nordlander
Copy link
Contributor

I agree. In fact, I originally implemented % formatting operator with the sole purpose of it being an internal translation target for f-strings. It never reached that far, though, so here we are stuck with just the old-fashioned solution as a result.

But support for f-strings should be pretty easy to add. And I think translating f-strings into % expressions is still a good idea, so I suggest we keep this form internally even if we remove it from the front-end language.

An additional thought: do we really need to mark f-strings with the prefix character f"..."? Can't we just sat that the new style of formatting is universally available? We still have the raw strings r"..." for the rare cases where formatting is undesirable.

@plajjan
Copy link
Contributor Author

plajjan commented Dec 30, 2024

Yeah I agree. I think we should do formatting per default for strings and use r"" for raw unformatted strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request syntax Related to the Acton language syntax / design
Projects
None yet
Development

No branches or pull requests

2 participants