4 -- with var_1, var_2... = val_1, val_2... do
8 -- will assign val_n to var_n foreach n, and guaranty that var_n:close() will be called,
9 -- no matter what, even if the body causes an error, even if it returns, even
10 -- if another :close() call causes an error, etc. No. Matter. What.
12 require 'metalua.extension.trycatch'
14 function withdo_builder (x)
15 local names, vals, body = unpack(x)
16 for i = #names, 1, -1 do
17 local name, val = names[i], vals[i]
18 body = trycatch_builder{ { `Set{ {name}, {val} }, body }, -- try-block
20 { +{ print ("closing "..-{`String{name[1]}}) },
21 `Invoke{ name, `String "close" } } }
23 table.insert(body, 1, `Local{ names })
29 'with', mlp.id_list, '=', mlp.expr_list, 'do', mlp.block, 'end',
30 builder = withdo_builder }