[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: curious why private variables are rare
From: |
Emanuel Berg |
Subject: |
Re: curious why private variables are rare |
Date: |
Mon, 21 Nov 2022 13:48:50 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
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