Skip to content

Commit 369e845

Browse files
committed
Update walrus operator
1 parent 254ada2 commit 369e845

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

docs/New Operators.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ This operator does not implement any metamethods.
105105

106106
## Walrus Operator
107107
The Walrus operator allows you to perform assignments inside of conditional expresssions.
108-
109108
```pluto
110109
local get_value = || -> 1
111110
@@ -116,7 +115,7 @@ else -- scope of 'val' ends
116115
end
117116
```
118117

119-
Note that for while-loops, it will be executed as many times as the condition:
118+
It can also be used in while loops, where it will be executed as many times as the condition:
120119
```pluto norun title="Pluto Way"
121120
while a := next_value() do
122121
-- ...
@@ -130,6 +129,22 @@ while true do
130129
end
131130
```
132131

132+
In both cases, it is not limited to initialize only a single variable, although only the first is checked for truthiness:
133+
```pluto
134+
local co = coroutine.create(function()
135+
while true do
136+
while _has_val, val := coroutine.yield() do
137+
print(val)
138+
end
139+
end
140+
end)
141+
co:resume() -- start coroutine
142+
co:resume(true, 1) --> 1
143+
co:resume(false)
144+
co:resume(true, 2) --> 2
145+
co:resume(true, nil) --> nil
146+
```
147+
133148
## Spaceship Operator
134149
The spaceship operator, also known as the three-way comparison operator, allows you to quickly compare 2 values for equality and order.
135150

0 commit comments

Comments
 (0)