zsh-workers
 help / color / mirror / code / Atom feed
* A tip: a technique to limit the # of global variables
@ 2019-10-11  8:20 Sebastian Gniazdowski
  2019-10-11  8:26 ` Roman Perepelitsa
  0 siblings, 1 reply; 2+ messages in thread
From: Sebastian Gniazdowski @ 2019-10-11  8:20 UTC (permalink / raw)
  To: Zsh hackers list

Hello,
when writing a plugin one often needs to keep a state during the Zsh
session. To do this it is natural to use global parameters. However,
when the number of the parameters grows one might want to limit it.

I've discovered a way to do this. Only a single global parameter per
plugin might be needed with it:


typeset -A PLGMAP
typeset -A some_map
typeset -a some_array

# Use
PLGMAP[state]=1
some_map[state]=1
some_array[1]=state

can be converted into:

typeset -A PLGMAP

# Use
PLGMAP[state]=1
PLGMAP[some_map__state]=1
PLGMAP[some_array__1]=state

i've did this in Zplugin reducing the # of global parameters by 21.
It's really unproblematic, I was using vim patterns with back
references to do the conversion without any back firing.

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: A tip: a technique to limit the # of global variables
  2019-10-11  8:20 A tip: a technique to limit the # of global variables Sebastian Gniazdowski
@ 2019-10-11  8:26 ` Roman Perepelitsa
  0 siblings, 0 replies; 2+ messages in thread
From: Roman Perepelitsa @ 2019-10-11  8:26 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh hackers list

On Fri, Oct 11, 2019 at 10:21 AM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> Hello,
> when writing a plugin one often needs to keep a state during the Zsh
> session. To do this it is natural to use global parameters. However,
> when the number of the parameters grows one might want to limit it.

What are the advantages of having fewer global parameters? Why is it
better to have this:

    typeset -gA PLGMAP=(foo 42 bar hello)

Than this:

    typeset -gi PLGMAP_foo=42
    typeset -g  PLGMAP_bar=hello

Roman.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-10-11  8:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-11  8:20 A tip: a technique to limit the # of global variables Sebastian Gniazdowski
2019-10-11  8:26 ` Roman Perepelitsa

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).