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

RFC: Format Specification for String Interpolation #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/string-interpolation-format-specification.md
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`
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor

@Kampfkarren Kampfkarren Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local function getString(thing: T, padding: number?)
    local text = thing.name
    if padding ~= nil then
         text = string.rep(' ', padding) .. text
    end
    return text
end

setmetatable(Thing, {
    __tostring = getString,
})

-- Other calls
print(getString(thing, 4))

...would error with {thing,f}, though this isn't really backwards incompatible as it still requires you actually use the new specification.

Copy link
Contributor

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 discards y 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.

local t = setmetatable({ 1, 2, 3, 5 }, {
  __tostring = table.concat
})

-- before this RFC:
print(tostring(t)) -- 1235
print(tostring(t, ",")) -- 1235
print(`{t,,}`) -- illegal

-- after this RFC:
print(tostring(t)) -- 1235
print(tostring(t, ",")) -- 1,2,3,5 (behavior changed)
print(`{t,,}`) -- 1,2,3,5

But it's not backward incompatible via format specifiers in string interpolation syntax because it's syntactically invalid to even write one right now.