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: String interpolation #165

Merged
merged 8 commits into from Nov 22, 2021
Merged

RFC: String interpolation #165

merged 8 commits into from Nov 22, 2021

Conversation

alexmccord
Copy link
Member

@alexmccord alexmccord added the rfc Language change proposal label Nov 7, 2021
rfcs/syntax-string-interpolation.md Show resolved Hide resolved
rfcs/syntax-string-interpolation.md Outdated Show resolved Hide resolved
rfcs/syntax-string-interpolation.md Show resolved Hide resolved
rfcs/syntax-string-interpolation.md Outdated Show resolved Hide resolved
rfcs/syntax-string-interpolation.md Outdated Show resolved Hide resolved
rfcs/syntax-string-interpolation.md Outdated Show resolved Hide resolved
Also adds a bunch of other things that was missing entirely.
@alexmccord alexmccord changed the title RFC: String interpolation and extending string.format RFC: String interpolation Nov 9, 2021
@matthargett
Copy link

Would this support inline if-expressions? eg

print(`The lock combinations are: {if combo then table.concat(combo, ", ") else "unknown"}`)

@alexmccord
Copy link
Member Author

It would. Any arbitrary expressions will fly.

@luau-lang luau-lang deleted a comment from arch-glitch Nov 20, 2021
@zeux
Copy link
Collaborator

zeux commented Nov 20, 2021

I've been thinking about the delimiter syntax and was wondering about backticks vs $". The RFC has a justification for the backtick in terms of implementation but I'm not sure I fully understand it -- the starting token presumably doesn't matter because it's special, is the difference in the complexity of processing unadorned " when inside an interpolated string?

The benefit of $" is that it might be more compatible with existing syntax highlighters that don't support this syntax.

@Halalaluyafail3
Copy link
Contributor

Halalaluyafail3 commented Nov 21, 2021

I dislike the $"..." syntax because it isn't clear if a space can be inserted between $ and "..." (it looks like two different tokens that may have spaces between them), and it is confusing when used to call a function without parentheses (foo$"..." looks like a $ operator).

Having a syntax that affixes a dollar sign to a string seems to imply that all string literals would work, specifically the single quote ('...') and long bracketed strings ([[...]], [=[...]=], etc.), although long strings don't make much sense since they don't interpret escape sequences. If the $"..." syntax was chosen, would $'...' and $[[...]] work?

The benefit of having it compatible with existing syntax highlighters only makes sense if the contents of the string wouldn't have any quotes or backslashes inside of an expression, for example $"a = {string.format("%i",a)}; b = {string.format("%i",b)};" would break existing syntax highlighters.

@zeux
Copy link
Collaborator

zeux commented Nov 21, 2021

Yeah the embedded " is a good point. Backtick it is then...

@alexmccord
Copy link
Member Author

fwiw, if you want your string to span multiple lines, we could extend backticks to also work with that? I think that is a backward compatible addition? I can add this into the RFC saying that we may at some point extend backticks to work across multiple lines.

@zeux
Copy link
Collaborator

zeux commented Nov 22, 2021

I don't think we need to amend the RFC for the future possibility of the multi-line literals - if we decide to extend this in the future we can just make a new RFC.

@zeux zeux merged commit 6958716 into master Nov 22, 2021
@zeux zeux deleted the alexmccord-patch-1 branch November 22, 2021 22:59
@Kampfkarren Kampfkarren mentioned this pull request Jul 27, 2022
7 tasks
alexmccord added a commit that referenced this pull request Aug 24, 2022
Implements the string interpolation RFC (#165).

Adds the string interpolation as per the RFC.

```lua
local name = "world"
print(`Hello {name}!`) -- Hello world!
```

Co-authored-by: Arseny Kapoulkine <arseny.kapoulkine@gmail.com>
Co-authored-by: Alexander McCord <11488393+alexmccord@users.noreply.github.com>
RomanKhafizianov pushed a commit to RomanKhafizianov/luau that referenced this pull request Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rfc Language change proposal
Development

Successfully merging this pull request may close these issues.

None yet

5 participants