zsh-users
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: "René 'Necoro' Neumann" <lists@necoro.eu>
Cc: zsh-users@zsh.org
Subject: Re: Local inner functions
Date: Thu, 24 Mar 2011 12:51:19 +0100	[thread overview]
Message-ID: <AANLkTikWnp=8wbWujGok_Fuu6M4JrHzw_Ornt=d__VJA@mail.gmail.com> (raw)
In-Reply-To: <4D8B1128.2090405@necoro.eu>

On 24 March 2011 10:38, René 'Necoro' Neumann <lists@necoro.eu> wrote:
> Am 24.03.2011 02:41, schrieb Bart Schaefer:
>> On Wed, Mar 23, 2011 at 4:23 PM, René 'Necoro' Neumann
> <lists@necoro.eu> wrote:
>>>
>>> foo ()
>>> {
>>>   bar () { }
>>> }
>>>
>>> Is there some way of making 'bar' to be local to 'foo'? Using the
>>> 'local' keyword does not work :).
>>
>> The short answer is "no."  And you can't make local aliases either.
>>
>> The slightly longer answer is that there are a couple of ways to fudge
>> it, of varying degrees of hackishness.
>
> Thanks for your answer. Instead of your 'always'-block, I tried
> trap 'unfunction bar' EXIT - but of course this is no good in case of
> name clashes :).
>
> So, if nothing like this really works (and local functions are not been
> to be implemented into zsh), I'll just use the $0_bar approach (wasn't
> aware of this), to at least reduce the chance of clashes.

If your function doesn't need to modify the parent environment in any
way, you can simply run it in a subshell:

function bar() { echo stuff }
function foo() {
 (
  function bar() { echo whatever }
  bar
 )
}
foo
bar

-- 
Mikael Magnusson


      reply	other threads:[~2011-03-24 11:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-23 23:23 René 'Necoro' Neumann
2011-03-24  1:16 ` nix
2011-03-24  8:50   ` René 'Necoro' Neumann
2011-03-24  1:41 ` Bart Schaefer
2011-03-24  9:38   ` René 'Necoro' Neumann
2011-03-24 11:51     ` Mikael Magnusson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTikWnp=8wbWujGok_Fuu6M4JrHzw_Ornt=d__VJA@mail.gmail.com' \
    --to=mikachu@gmail.com \
    --cc=lists@necoro.eu \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).