-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
News about string interpolation feature and a syntax page update (#829)
News can be viewed [here](https://vegorov-rbx.github.io/luau/2023/02/02/luau-string-interpolation.html) And syntax can be viewed [here](https://vegorov-rbx.github.io/luau/syntax#string-interpolation) Note that link appears to be broken in the news section. It goes to `https://vegorov-rbx.github.io/syntax#string-interpolation` instead of `https://vegorov-rbx.github.io/luau/syntax#string-interpolation` The link I use in the source is `/syntax#string-interpolation` but the root 'offset' is different on Luau website (doesn't have extra 'luau' folder) and on my GitHub pages. Hope this is ok. But if GitHub page masters know how to avoid this, please let me know. --------- Co-authored-by: Alan Jeffrey <[email protected]> Co-authored-by: Alexander McCord <[email protected]>
- Loading branch information
1 parent
62483d4
commit c5555c6
Showing
2 changed files
with
87 additions
and
0 deletions.
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
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,33 @@ | ||
--- | ||
layout: single | ||
title: "String Interpolation" | ||
--- | ||
|
||
String interpolation is the new syntax introduced to Luau that allows you to create a string literal with expressions inside of that string literal. | ||
|
||
In short, it's a safer and more ergonomic alternative over `string.format`. | ||
|
||
Here's a quick example of a string interpolation: | ||
|
||
```lua | ||
local combos = {2, 7, 1, 8, 5} | ||
print(`The lock combination is {table.concat(combos)}. Again, {table.concat(combos, ", ")}.`) | ||
--> The lock combination is 27185. Again, 2, 7, 1, 8, 5. | ||
``` | ||
|
||
String interpolation also composes well with the `__tostring` metamethod. | ||
|
||
```lua | ||
local balance = setmetatable({ value = 500 }, { | ||
__tostring = function(self) | ||
return "$" .. tostring(self.value) | ||
end | ||
}) | ||
|
||
print(`You have {balance}!`) | ||
--> You have $500! | ||
``` | ||
|
||
To find out more details about this feature, check out [Luau Syntax page](/syntax#string-interpolation). | ||
|
||
This is also the first major language feature implemented in a [contribution](https://github.com/Roblox/luau/pull/614) from the open-source community. Thanks [Kampfkarren](https://github.com/Kampfkarren)! |