Zulip Chat Archive

Stream: general

Topic: try, salvage, catch


Thomas Murrills (Mar 14 2024 at 03:37):

Ever wanted to grab some data dependent (only) on the monad state after you've thrown an error in a try block? Well...probably not, actually. But if you ever find yourself in that specific predicament, here's some code that lets you write the following in most of Lean's major monads. :)

  try
    ...
  salvage ex =>
    ...
    -- salvage `b : β` by working in the monad after `ex` has been thrown,
    -- but before restoring the state with `catch`
    return b
  catch ex (b : β) =>
    <do something with b!>
    ...

(I'm not planning on PRing this—this is just for fun. Thought I'd share. :) )

code


Last updated: May 02 2025 at 03:31 UTC