Functions that update themselves

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Functions that update themselves

konsolebox
Hi Chet,

Do you explicitly allow functions to do this?  I just want to know.

a() { a() { ...; }; a; }
b() { unset -f b; command b "$@"; }

My current use case for this is to have a function simplify itself
when condition checks are no longer necessary and just adds
unnecessary runtime load.  I also had many other previous ones.

--
konsolebox

Reply | Threaded
Open this post in threaded view
|

Re: Functions that update themselves

Andreas Kusalananda Kähäri-3
On Mon, Jun 03, 2019 at 03:54:59PM +0800, konsolebox wrote:

> Hi Chet,
>
> Do you explicitly allow functions to do this?  I just want to know.
>
> a() { a() { ...; }; a; }
> b() { unset -f b; command b "$@"; }
>
> My current use case for this is to have a function simplify itself
> when condition checks are no longer necessary and just adds
> unnecessary runtime load.  I also had many other previous ones.
>
> --
> konsolebox

This does not seem to be a bug report. Maybe you meant to send this to
the help-bash list?

--
Kusalananda
Sweden

Reply | Threaded
Open this post in threaded view
|

Re: Functions that update themselves

Chet Ramey
In reply to this post by konsolebox
On 6/3/19 3:54 AM, konsolebox wrote:
> Hi Chet,
>
> Do you explicitly allow functions to do this?  

It's not disallowed. Since a function definition is a compound command,
and all functions are global, I don't see why it shouldn't.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    [hidden email]    http://tiswww.cwru.edu/~chet/

Reply | Threaded
Open this post in threaded view
|

Re: Functions that update themselves

konsolebox
On Mon, Jun 3, 2019 at 9:26 PM Chet Ramey <[hidden email]> wrote:
> It's not disallowed. Since a function definition is a compound command,
> and all functions are global, I don't see why it shouldn't.

What I worry about is the way an executable part of the function can
be freed while the function is being redefined or unset that it may
cause faults because the code is still being executed.  Bash doesn't
document its strategy for freeing up function code so whether it's
safe or not is currently ambiguous.  Perhaps having one last line of
code to execute after a function is redefined is currently safe, but
it would be nice if it gets affirmed that it's intended to be.

--
konsolebox

Reply | Threaded
Open this post in threaded view
|

Re: Functions that update themselves

Chet Ramey
On 6/3/19 10:46 AM, konsolebox wrote:

> On Mon, Jun 3, 2019 at 9:26 PM Chet Ramey <[hidden email]> wrote:
>> It's not disallowed. Since a function definition is a compound command,
>> and all functions are global, I don't see why it shouldn't.
>
> What I worry about is the way an executable part of the function can
> be freed while the function is being redefined or unset that it may
> cause faults because the code is still being executed.  Bash doesn't
> document its strategy for freeing up function code so whether it's
> safe or not is currently ambiguous.  Perhaps having one last line of
> code to execute after a function is redefined is currently safe, but
> it would be nice if it gets affirmed that it's intended to be.

The function body gets copied before being executed, so the flags and
commands can be modified, just like any other command. I don't see this
ever changing. That's an implementation detail, though.


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    [hidden email]    http://tiswww.cwru.edu/~chet/