-
Notifications
You must be signed in to change notification settings - Fork 27
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
RFC: Format Specification for String Interpolation #20
Open
menarulalam
wants to merge
3
commits into
master
Choose a base branch
from
string-interpolation-format-specification
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Format Specification for String Interpolation | ||
|
||
## Summary | ||
|
||
This feature allows us to give format specifiers when printing interpolated strings such as `.2f` for a float rounded to two decimal places or `x` for printing in hex. It has all of the format specifiers available in `string.format`. It is delimited by a `,` in interpolated strings. | ||
|
||
## Motivation | ||
|
||
Most languages allow for format specifiers such as python f-strings or printf in C. Lua also supports `string.format` but we want to make it easier to use with interpolated strings. | ||
|
||
## Design | ||
|
||
Under the hood, this will just be syntactic sugar in addition to the current implementation of interpolated strings. Before, interpolated strings were passed to `string.format` with no format was specified, but now we will also pass the optional format specifier. We decided on `,` to act as the delimiter for format specification. | ||
|
||
To give some examples, this is how it would look like in code: | ||
|
||
```lua | ||
balance = 100.2035 | ||
print(`You have ${balance,.2f} in your bank account`) | ||
``` | ||
`You have $100.20 in your bank account` | ||
|
||
```lua | ||
number = 12345 | ||
print(`12345 is 0x{number,x} in hex!`) | ||
``` | ||
`12345 is 0x3039 in hex!` | ||
|
||
This will support most additions that could be made to `string.format` in the future as well. | ||
|
||
## Drawbacks | ||
|
||
There are no clear drawbacks to this. | ||
|
||
## Alternatives | ||
|
||
We have also considered allowing arbitrary format specifiers and not just ones supported by `string.format`. We would allow `__tostring` to take a second format argument, and that specifier get passed into it. We determined that we won't do this for now because it could mess up backwards compatibility on existing `__tostring` calls and a performance regression since we can't assume `tostring` will only have one argument anymore. Also for specifiers that work with `string.format` will be slower than just using `string.format` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no backward compatibility concerns I'm aware of? Can you provide an example where such code would break?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretically if a 2nd argument is supplied to tostring, it will be used in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...would error with
{thing,f}
, though this isn't really backwards incompatible as it still requires you actually use the new specification.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess technically you could write
tostring(x, y)
and it'd work just fine and discardsy
and indeed, adding this feature would turn a dormant bug into a breaking bug. I still consider that an extremely unlikely event except in the case of variadics which makes it look inconspicuous at first. Something as weird as this most certainly would change behavior.But it's not backward incompatible via format specifiers in string interpolation syntax because it's syntactically invalid to even write one right now.