mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 04:24:43 +00:00
43 lines
No EOL
1.3 KiB
Markdown
43 lines
No EOL
1.3 KiB
Markdown
# 擴展語法
|
||
|
||
[](https://gezf7g7pd5.execute-api.ap-northeast-1.amazonaws.com/default/source_up_to_date?owner=erg-lang&repos=erg&ref=main&path=doc/EN/syntax/29_spread_syntax.md&commit_hash=e959b3e54bfa8cee4929743b0193a129e7525c61)
|
||
|
||
在分解賦值中,將 `...` 放在變量前面會將所有剩余元素展開到該變量中。這稱為展開賦值
|
||
|
||
```python
|
||
[x,...y] = [1, 2, 3]
|
||
assert x == 1
|
||
assert y == [2, 3]
|
||
x, ...y = (1, 2, 3)
|
||
assert x == 1
|
||
assert y == (2, 3)
|
||
```
|
||
|
||
## 提取賦值
|
||
|
||
提取分配是一種方便的語法,用于本地化模塊或記錄中的特定屬性
|
||
|
||
```python
|
||
{sin; cos; tan} = import "math"
|
||
```
|
||
|
||
之后,您可以在本地使用`sin,cos,tan`
|
||
|
||
您可以對記錄執行相同的操作。
|
||
|
||
```python
|
||
record = {x = 1; y = 2}
|
||
{x; y} = record
|
||
```
|
||
|
||
如果要全部展開,請使用`{*}=record`。它在OCaml中是`open`。
|
||
|
||
```python
|
||
record = {x = 1; y = 2}
|
||
{*} = records
|
||
assert x == 1 and y == 2
|
||
```
|
||
|
||
<p align='center'>
|
||
<a href='./28_comprehension.md'>上一頁</a> | <a href='./30_decorator.md'>下一頁</a>
|
||
</p> |