This is taken from javascript documentation.
Spread syntax looks exactly like rest syntax. In a way, spread syntax is the opposite of rest syntax. Spread syntax "expands" an array into its elements, while rest syntax collects multiple elements and "condenses" them into a single element.
...expr
- This is probably best because it mirrors the rest operator
...$var
- Referencing a variable and spreading it (used in examples below)*expr
expr*
..
- Because match
uses ..
..$rest
rather than ...$rest
to mirror parameters (I guess that's because that's what Rust does?)...
or*
, then something like { ... (expr) }
could be intended as a closure that calls ...
on (expr)
.
^...
match
uses ..
to match the remaining elements of lists and records. The fact that it's different might confuse people....
only recognized before $variables
, [lists]
, and (subexpressions)
(no whitespace in between)...
outside listsThese examples are just thought starters. Nothing is "in stone" yet.
Spread the $rest
of parameters to command line arguments for external applications.
def test [...rest] {
^external_cmd ...$rest
}
Spread the list into a command (nushell already does this)
let numbers = [1 2 3]
$numbers | math sum
Spread the list into variables (not sure if this destructuring is included)
let numbers = [1 2 3 4]
let (a b c d) = ...$numbers
$a = 1
$b = 2
$c = 3
$d = 4
Spread the string into a record?
let s = ..."test"
{"0": "t", "1": "e", "2": "s", "3": "t"}
Note: spreading strings not supported, at least temporarily, as ambiguity about how to split
Spread list into another list?
let parts = ["shoulders", "knees"];
let lyrics = ["head", ...$parts, "and", "toes"];
// ["head", "shoulders", "knees", "and", "toes"]
Spread list of lists into a table?
let list2d = [[1 2] [3 4]];
let table = [[x y]; ...$list2d];
// [[x y]; [1 2] [3 4]]
Error at runtime if too many columns
let list2d = [[1 2] [3 4 5 6]];
let table = [[x y]; ...$list2d];
// Error: too many columns, expected 2 but got 4
Error at runtime if too few columns (alternative: put in nulls)
let list2d = [[1 2] [3 4 5 6]];
let table = [[w x y z]; ...$list2d];
// Error: too few columns, expected 4 but got 2
Bare strings
[...echo foo]
// Either [{"0": "e", "1": "c", "2": "h", "3": "o"} "foo"]
// or [["e", "c", "h", "o"] "foo"]
// (see example 4)
Note: spreading strings not supported, at least temporarily, as ambiguity about how to split
Passing arguments to fill in rest parameters (like Example 1)
def foo [bar: string, ...baz: string] {}
foo "quux" "xyzzy" ...$args "last"
// $baz would contain "xyzzy", the items in $args, and "last"
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing