From fe996a2f81c00f648923624adedc2a52950454f9 Mon Sep 17 00:00:00 2001 From: Menarul Alam Date: Wed, 10 Jan 2024 11:58:31 -0800 Subject: [PATCH 1/3] Initial commit --- ...ring-interpolation-format-specification.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 docs/string-interpolation-format-specification.md diff --git a/docs/string-interpolation-format-specification.md b/docs/string-interpolation-format-specification.md new file mode 100644 index 00000000..a0416ca1 --- /dev/null +++ b/docs/string-interpolation-format-specification.md @@ -0,0 +1,35 @@ +# 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 speified, 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!` + +## 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` From 2695dd2ca848365eca4d269b1c8f4b0e82c9a0f0 Mon Sep 17 00:00:00 2001 From: Menarul Alam Date: Wed, 10 Jan 2024 12:02:44 -0800 Subject: [PATCH 2/3] Added a sentence --- docs/string-interpolation-format-specification.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/string-interpolation-format-specification.md b/docs/string-interpolation-format-specification.md index a0416ca1..1f16b685 100644 --- a/docs/string-interpolation-format-specification.md +++ b/docs/string-interpolation-format-specification.md @@ -12,7 +12,7 @@ Most languages allow for format specifiers such as python f-strings or printf in 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 speified, 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 +To give some examples, this is how it would look like in code: ```lua balance = 100.2035 @@ -26,6 +26,8 @@ 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. From a50bbbf0b94c1988b0b6538999b6841ba4e58109 Mon Sep 17 00:00:00 2001 From: menarulalam <35981995+menarulalam@users.noreply.github.com> Date: Thu, 11 Jan 2024 13:54:56 -0800 Subject: [PATCH 3/3] Update docs/string-interpolation-format-specification.md Co-authored-by: Alexander McCord <11488393+alexmccord@users.noreply.github.com> --- docs/string-interpolation-format-specification.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/string-interpolation-format-specification.md b/docs/string-interpolation-format-specification.md index 1f16b685..bb3a3f68 100644 --- a/docs/string-interpolation-format-specification.md +++ b/docs/string-interpolation-format-specification.md @@ -10,7 +10,7 @@ Most languages allow for format specifiers such as python f-strings or printf in ## 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 speified, but now we will also pass the optional format specifier. We decided on `,` to act as the delimiter for format specification. +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: