elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.53k stars 297 forks source link

Document the exception behaviour of defer #1687

Open hanche opened 1 year ago

hanche commented 1 year ago

The current documentation for defer says only this about exceptions:

any exception it throws gets propagated

Experimenting a bit reveals two undocumented facts:

Examples:

~> { defer { put done }; fail foo }
▶ done
Exception: foo
Traceback:
  [tty 1]:1:23:
    { defer { put done }; fail foo }
  [tty 1]:1:1:
    { defer { put done }; fail foo }
~> { defer { put done; fail oops }; fail foo }
▶ done
Exception: foo
Traceback:
  [tty 2]:1:34:
    { defer { put done; fail oops }; fail foo }
  [tty 2]:1:1:
    { defer { put done; fail oops }; fail foo }

(I had somehow managed to miss defer until now, which is why I felt a need to play with it.)

hanche commented 6 months ago

One more defer behaviour that merits documentation: If a closure contains several defer calls, their functions will be called in the reverse order, from the last to the first one.