From: Emanuel Berg <incal@dataswamp.org>
To: help-gnu-emacs@gnu.org
Subject: Re: curious why private variables are rare
Date: Mon, 21 Nov 2022 13:48:50 +0100 [thread overview]
Message-ID: <874jus3231.fsf@dataswamp.org> (raw)
In-Reply-To: CAJcAo8tsy-pFFZ2L2FTO=P71NVsPKDX20KmXnzgO5GdVf9O44A@mail.gmail.com
Samuel Wales wrote:
> the usual thing is:
>
> (defvar var ...)
> (defun fun ...)
I hope not, as that introduces a global/dynamic/special
variable ...
> even when var is only used by fun. but you could do:
>
> (let ((var ...))
> (defun fun ...))
Yes, a lexical let-closure.
Here [1] is an example of the two use cases I've found so far
https://dataswamp.org/~incal/emacs-init/w3m/w3m-survivor.el
The use-cases are
(1) state variables that don't change between function calls
(2) share variables between functions
(The `declare-function' stuff is to shut up the byte-compiler.)
> [at least, i /think/ you can do this with similar results from the
> perspective of the function
You think right!
> the most obvious drawback of the latter would probably be
> convenience in debugging/inspectability/discoverability.
> re-using a variable name could be confusing. other than that
> and extra indentation, i'm not sure if there are
> big drawbacks.
I don't think there are any drawbacks to closures, it's a good
way, maybe the best way, to do (1) and (2).
> it would limit scope so you don't pollute completion,
> apropos, etc. you can eliminate prefix. no stomp on vars.
If so, that's a problem for completion etc.
[1] https://dataswamp.org/~incal/emacs-init/w3m/w3m-survivor.el
--
underground experts united
https://dataswamp.org/~incal
prev parent reply other threads:[~2022-11-21 12:48 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-20 2:07 curious why private variables are rare Samuel Wales
2022-11-20 3:53 ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-11-21 12:52 ` Emanuel Berg
2023-10-02 23:56 ` Samuel Wales
2022-11-21 12:48 ` Emanuel Berg [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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=874jus3231.fsf@dataswamp.org \
--to=incal@dataswamp.org \
--cc=help-gnu-emacs@gnu.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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).