bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: feature request: new builtin `defer`, scope delayed eval


From: Lawrence Velázquez
Subject: Re: feature request: new builtin `defer`, scope delayed eval
Date: Thu, 06 Oct 2022 18:03:44 -0400
User-agent: Cyrus-JMAP/3.7.0-alpha0-1015-gaf7d526680-fm-20220929.001-gaf7d5266

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

[Prev in Thread] Current Thread [Next in Thread]