mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-30 21:01:10 +00:00
45 lines
1.6 KiB
Markdown
45 lines
1.6 KiB
Markdown
# Spread assignment
|
|
|
|
[
|
|
](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/28_spread_syntax.md&commit_hash=6dc8c5015b6120497a26d80eaef65d23eb2bee2a)
|
|
|
|
In a spread assignment, a variable can be prefixed with `...` in front of the variable, all the remaining elements can be expanded into the variable. This is called a spread assignment.
|
|
|
|
```erg
|
|
[x, ... .y] = [1, 2, 3]
|
|
assert x == 1
|
|
assert y == [2, 3].
|
|
x, ... .y = (1, 2, 3)
|
|
assert x == 1
|
|
assert y == (2, 3)
|
|
```
|
|
|
|
## Extract assignment
|
|
|
|
If nothing is written after `...`, the remaining elements are ignored and an assignment is made. This type of expansion assignment is specifically called an extract assignment.
|
|
Extract assignment is a useful syntax for bringing certain attributes local to a module or record.
|
|
|
|
```erg
|
|
{sin; cos; tan; ...} = import "math"
|
|
```
|
|
|
|
This way, `sin`, `cos`, `tan` can be used locally from then on.
|
|
|
|
You can do the same with records.
|
|
|
|
```erg
|
|
record = {x = 1; y = 2}
|
|
{x; y; ...} = record
|
|
```
|
|
|
|
If you want to expand all of them, use `{*} = record`, this is equivalent to `open` in OCaml and so on.
|
|
|
|
```erg
|
|
record = {x = 1; y = 2}
|
|
{*} = record
|
|
assert x == 1 and y == 2
|
|
```
|
|
|
|
<p align='center'>
|
|
<a href='./27_comprehension.md'>Previous</a> | <a href='./29_decorator.md'>Next</a>
|
|
</p>
|