* Obtaining the names of all local variables
@ 2015-06-25 15:46 Bart Schaefer
2015-06-27 21:45 ` Oliver Kiddle
0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2015-06-25 15:46 UTC (permalink / raw)
To: zsh-workers
Oliver mentioned:
> I did something like the following in _git recently. It works
> arr=( one two three )
> local $arr
This got me thinking, wouldn't it be helpful if zsh/parameter had an array
$locals that holds the names of all the local parameters? Then you could
needs_private_scope() { local -h $locals; ... }
It then occurred to me that it might be more natural to have "local" handle
this. But:
torch% () { local +m \* }
(anon): bad option: -m
Of course there's an ambiguity in both cases; given
typeset bar
scope1 () {
local foo
}
scope2 () {
local foo bar
scope1
}
Should "bar" be considered local in scope1 when called from scope2? If not,
this whole idea is somewhat moot; you'd need a way to select which scope
you want to know all the locals in, which is ugly to contemplate.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Obtaining the names of all local variables
2015-06-25 15:46 Obtaining the names of all local variables Bart Schaefer
@ 2015-06-27 21:45 ` Oliver Kiddle
2015-06-27 23:08 ` Bart Schaefer
0 siblings, 1 reply; 3+ messages in thread
From: Oliver Kiddle @ 2015-06-27 21:45 UTC (permalink / raw)
To: zsh-workers
Bart wrote:
> This got me thinking, wouldn't it be helpful if zsh/parameter had an array
> $locals that holds the names of all the local parameters? Then you could
>
> needs_private_scope() { local -h $locals; ... }
I'm not sure I understand how that would achieve a private scope. -h is
only applicable to special parameters. You could do:
local -h ${(k)parameters[(R)*-special]}
Or you could mark every parameter local.
Oliver
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Obtaining the names of all local variables
2015-06-27 21:45 ` Oliver Kiddle
@ 2015-06-27 23:08 ` Bart Schaefer
0 siblings, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2015-06-27 23:08 UTC (permalink / raw)
To: zsh-workers
On Jun 27, 11:45pm, Oliver Kiddle wrote:
} Subject: Re: Obtaining the names of all local variables
}
} Bart wrote:
} > This got me thinking, wouldn't it be helpful if zsh/parameter had an array
} > $locals that holds the names of all the local parameters? Then you could
} >
} > needs_private_scope() { local -h $locals; ... }
}
} I'm not sure I understand how that would achieve a private scope.
A few weeks ago someone asked for what amounts to lexical scoping of
local variables instead of dynamic scoping. There's no way to achieve
that completely, but if you can hide all the variables that your
caller has made local, you get at least part of the way; you are then
guaranteed not to accidentally poke something into the caller's scope.
} -h is only applicable to special parameters.
Right, but if $locals contains a mix of non-special and special, you
(might) need the -h there. The real point is to make the non-specials
local as well, which this ...
} local -h ${(k)parameters[(R)*-special]}
... does not do, and besides that also makes the global specials local,
which is NOT the point.
} Or you could mark every parameter local.
That also hides the globals, which again is not the desired effect.
If there were a version of "typeset -g" that meant "expose the listed
top-level global variables, regardless of intervening dynamic scopes"
that would actually be a better starting point, but I think that's a
lot more difficult to achieve. Maybe I'm wrong.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-06-27 23:08 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-25 15:46 Obtaining the names of all local variables Bart Schaefer
2015-06-27 21:45 ` Oliver Kiddle
2015-06-27 23:08 ` Bart Schaefer
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).