Skip to content

Commit a157b62

Browse files
committed
Improve try()
1 parent ff6fee6 commit a157b62

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

docs/modules/pico8lib.functions.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ <h2><a href="#Local_Functions">Local Functions</a></h2>
9696
<td class="summary">Create a memoized version of a single parameter function</td>
9797
</tr>
9898
<tr>
99-
<td class="name" nowrap><a href="#try">try (t, c[, f])</a></td>
99+
<td class="name" nowrap><a href="#try">try (t[, c][, f], ...)</a></td>
100100
<td class="summary">try/catch/finally implemented using coroutine return codes</td>
101101
</tr>
102102
</table>
@@ -173,7 +173,7 @@ <h3>Returns:</h3>
173173
</dd>
174174
<dt>
175175
<a name = "try"></a>
176-
<strong>try (t, c[, f])</strong>
176+
<strong>try (t[, c][, f], ...)</strong>
177177
</dt>
178178
<dd>
179179
try/catch/finally implemented using coroutine return codes
@@ -188,14 +188,26 @@ <h3>Parameters:</h3>
188188
<li><span class="parameter">c</span>
189189
<span class="types"><span class="type">function</span></span>
190190
function to call if t raises an error
191+
(<em>optional</em>)
191192
</li>
192193
<li><span class="parameter">f</span>
193194
<span class="types"><span class="type">function</span></span>
194195
function to call if t or c succeeds without error
195196
(<em>optional</em>)
196197
</li>
198+
<li><span class="parameter">...</span>
199+
Additional parameters that will be passed to <code>t</code>, <code>c</code>, and <code>f</code>
200+
</li>
197201
</ul>
198202

203+
<h3>Returns:</h3>
204+
<ol>
205+
<li>
206+
<span class="types"><span class="type">bool</span></span>
207+
Did <code>t</code> exit without error?</li>
208+
<li>
209+
Error message or successful return value from <code>t</code></li>
210+
</ol>
199211

200212

201213

@@ -208,7 +220,7 @@ <h3>Parameters:</h3>
208220
</div> <!-- id="main" -->
209221
<div id="about">
210222
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
211-
<i style="float:right;">Last updated 2024-09-08 14:28:56 </i>
223+
<i style="float:right;">Last updated 2024-09-08 19:10:26 </i>
212224
</div> <!-- id="about" -->
213225
</div> <!-- id="container" -->
214226
</body>

pico8lib/functions.lua

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,26 @@ end
3535

3636
--- try/catch/finally implemented using coroutine return codes
3737
-- @tparam function t function to always call first
38-
-- @tparam function c function to call if t raises an error
38+
-- @tparam[opt] function c function to call if t raises an error
3939
-- @tparam[opt] function f function to call if t or c succeeds without error
40-
local function try(t, c, f)
40+
-- @param ... Additional parameters that will be passed to `t`, `c`, and `f`
41+
-- @treturn bool Did `t` exit without error?
42+
-- @return Error message or successful return value from `t`
43+
local function try(t, c, f, ...)
4144
-- originally from https://www.lexaloffle.com/bbs/?pid=72820
4245
-- "enjoy, and if you like it feel free to kick a buck to my patreon."
4346
-- https://www.patreon.com/sharkhugseniko
4447
local co = cocreate(t)
4548
local s, m = true
49+
-- This loop can be omitted and the contents run just once if `t` cannot contain `yield()`
4650
while s and costatus(co) ~= "dead" do
47-
s, m = coresume(co)
48-
if not s then
49-
c(m)
51+
s, m = coresume(co, ...)
52+
if not s and c then
53+
c(m, ...)
5054
end
5155
end
5256
if f then
53-
f()
57+
f(...)
5458
end
59+
return s, m
5560
end

0 commit comments

Comments
 (0)