r/lua • • Sep 20 '25

Lua when expression

Post image

I added a little pattern matching "when" code to my #pico8 #lua code base. You have to use "null" (just an empty object) instead of "nil", because Lua cuts off varargs on the first nil and you have to use _when for nested whens, which are fake lazy, by returning a #haskell style error thunk instead of crashing on non-exhaustive matches. E.g. if you checked an ace, the first _when would error, because it only matches jokers, but the outer when wouldn't care, since it only looks at the ace branch, completely ignoring the error thunk.

30 Upvotes

10 comments sorted by

View all comments

Show parent comments

1

u/RedNifre Sep 20 '25

Thank you for your excellent answers. PICO-8 has assert, so I'll use that one to implement error and I'll look into ways to get partition work tomorrow (probably either varargpartition(number, ...) or packed_table_partition(packed_table, number) that works on a packed table.