unpack syntax

This commit is contained in:
Daniel P H Fox (Roblox) 2025-01-29 12:15:13 -08:00 committed by GitHub
parent f6c14577c6
commit 3ef7e095a7
Signed by: DevComp
GPG key ID: B5690EEEBB952194

View file

@ -146,14 +146,12 @@ Then desugars again to:
foo, bar = data["foo"], data["bar"] foo, bar = data["foo"], data["bar"]
``` ```
#### Consecutive keys #### Unpacking
Consecutive keys can be implicitly expressed by dropping the key. Instead of listing out consecutive numeric keys, `unpack` can be used at the start of a matcher to implicitly key all subsequent items. This is useful for arrays and tuple-style tables.
*Open question: are we OK with this in the context of dot keys without names? Discuss in comments.*
```Lua ```Lua
{ foo, bar } { unpack foo, bar }
``` ```
This desugars once to: This desugars once to:
@ -164,10 +162,30 @@ This desugars once to:
Then desugars again to: Then desugars again to:
``` ```Lua
foo, bar = data[1], data[2] foo, bar = data[1], data[2]
``` ```
`unpack` skips dot keys and explicitly written keys:
```Lua
{ unpack foo, [10] = bar, baz, .garb }
```
This desugars once to:
```Lua
{ [1] = foo, [10] = bar, [2] = baz, ["garb"] = garb }
```
Then desugars again to:
```Lua
foo, bar, baz, garb = data[1], data[10], data[2], data["garb"]
```
It is invalid to specify an identifer without a key if `unpack` is not specified, for disambiguity with other languages.
#### Nested structure #### Nested structure
A structure matcher can be specified instead of an identifier, to match nested structure inside of that key. This is compatible with consecutive keys and dot keys. A structure matcher can be specified instead of an identifier, to match nested structure inside of that key. This is compatible with consecutive keys and dot keys.