r/lua • u/RedNifre • Sep 20 '25
Lua when expression
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
View all comments
Show parent comment
1
u/RedNifre Sep 21 '25
Thank you for your great help! I solved it by first replacing nil with null in the varargs when turning them into a table, turning null into a completely internal detail that the callers of when no longer have to worry about.