On Thu, Oct 6, 2022, at 4:08 PM, Cynthia Coan wrote:
> I'd specifically like to propose a new built-in called `defer` which
> acts like `eval` however is not parsed/expanded/run until it's scope
> is leaving.  Hopefully "scope" is the correct word, I'm imagining it
> running at the same time a local would go out of "scope" and be no
> longer available (just before the locals are cleared, so locals can
> still be used in expansion).

I think it would be more natural to implement function-local RETURN
and EXIT traps than introduce a second command that looks like
'trap' and quacks like 'trap' but is actually not 'trap'.  This
could be done generically by adding the ability to "scope" traps
to functions (possibly via a new option to 'trap' or a shopt à la
zsh's LOCAL_TRAPS) or specifically by creating "local" variants of
RETURN and EXIT.  Usage might look like this:

    f() {
        # new option
        trap -f 'cleaning up' EXIT RETURN
        cmd1
        cmd2
        cmd3
    }

or this:

    g() {
        # new traps
        trap 'cleaning up' EXIT_LOCAL RETURN_LOCAL
        cmdA
        cmdB
        cmdC
    }

-- 
vq

Reply via email to