mirror of
https://github.com/luau-lang/luau.git
synced 2025-05-04 10:33:46 +01:00
Final pages
This commit is contained in:
parent
a6b5dc74c0
commit
1e967186ae
2 changed files with 53 additions and 9 deletions
|
@ -222,3 +222,54 @@ The default iteration order for tables is specified to be consecutive for elemen
|
|||
|
||||
## String interpolation
|
||||
|
||||
Luau adds additional way to define string values that allows you to place runtime expressions directly inside specific spots of the literal.
|
||||
|
||||
This is a more ergonomic alternative over using `string.format` or `("literal"):format`.
|
||||
|
||||
To use string interpolation, use a backtick string literal:
|
||||
|
||||
```lua
|
||||
local count = 3
|
||||
print(`Bob has {count} apple(s)!`)
|
||||
--> Bob has 3 apple(s)!
|
||||
```
|
||||
|
||||
Any expression can be used inside `{}`:
|
||||
|
||||
```lua
|
||||
local combos = {2, 7, 1, 8, 5}
|
||||
print(`The lock combination is {table.concat(combos)}.`)
|
||||
--> The lock combination is 27185.
|
||||
```
|
||||
|
||||
Inside backtick string literal, `\` is used to escape `` ` ``, `{`, `\` itself and a newline:
|
||||
|
||||
```lua
|
||||
print(`Some example escaping the braces \{like so}`)
|
||||
--> Some example escaping the braces {like so}
|
||||
|
||||
print(`Backslash \ that escapes the space is not a part of the string...`)
|
||||
--> Backslash that escapes the space is not a part of the string...
|
||||
|
||||
print(`Backslash \\ will escape the second backslash...`)
|
||||
--> Backslash \ will escape the second backslash...
|
||||
|
||||
print(`Some text that also includes \`...`)
|
||||
--> Some text that also includes `...
|
||||
|
||||
local name = "Luau"
|
||||
|
||||
print(`Welcome to {
|
||||
name
|
||||
}!`)
|
||||
--> Welcome to Luau!
|
||||
```
|
||||
|
||||
### Restrictions and limitations
|
||||
|
||||
The sequence of two opening braces (`{{`) is rejected with a parse error.
|
||||
This restriction is made to prevent developers using other programming languages with a similar feature from trying to attempt that as a way to escape a single `{` and getting unexpected results in Luau.
|
||||
|
||||
Luau currently does not support backtick string literals as a type annotation, so ``type Foo = `Foo` `` is invalid.
|
||||
|
||||
Function calls with a backtick string literal without parenthesis is not supported, so ``print`hello` `` is invalid.
|
||||
|
|
|
@ -28,13 +28,6 @@ print(`You have {balance}!`)
|
|||
--> You have $500!
|
||||
```
|
||||
|
||||
Known Limitations
|
||||
Script editor will not support auto-formatting string interpolations.
|
||||
To find out more details about this feature, check out [Luau Syntax page](syntax).
|
||||
|
||||
Luau currently does not define any format specifiers for string interpolations.
|
||||
|
||||
Luau currently does not support any_function_call`with backticks without parentheses`.
|
||||
|
||||
Luau currently does not support backtick string literals as a type annotation, so type Foo = `Foo` is invalid.
|
||||
|
||||
Please post any comments or concerns you may have!
|
||||
This is also the first major language feature implemented by a contribution from the open-source community!
|
||||
|
|
Loading…
Add table
Reference in a new issue