unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* curious why private variables are rare
@ 2022-11-20  2:07 Samuel Wales
  2022-11-20  3:53 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-11-21 12:48 ` Emanuel Berg
  0 siblings, 2 replies; 5+ messages in thread
From: Samuel Wales @ 2022-11-20  2:07 UTC (permalink / raw)
  To: help-gnu-emacs

the usual thing is:

  (defvar var ...)
  (defun fun ...)

even when var is only used by fun.  but you could do:

  (let ((var ...))
    (defun fun ...))

[at least, i /think/ you can do this with similar results from the
perspective of the function, and it wfm.  however, i know that there
are multiple interpretations of lexical binding, at least at top
level, and those considerations might apply.]

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.

it would limit scope so you don't pollute completion, apropos, etc.
you can eliminate prefix.  no stomp on vars.

so i am just curious why the usual thing is usual.  is it the above
reasons?  or am i missing some bigger things?

-- 
The Kafka Pandemic

A blog about science, health, human rights, and misopathy:
https://thekafkapandemic.blogspot.com



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

* Re: curious why private variables are rare
  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
  2022-11-21 12:48 ` Emanuel Berg
  1 sibling, 1 reply; 5+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-11-20  3:53 UTC (permalink / raw)
  To: help-gnu-emacs

> even when var is only used by fun.  but you could do:
>
>   (let ((var ...))
>     (defun fun ...))

Indeed.

> so I am just curious why the usual thing is usual.  is it the above
> reasons?  or am i missing some bigger things?

Most likely it's because it started working quite late in the history of
ELisp, so habits were taken at a stage where it wasn't an option.

Side note: the above works, but the compiler doesn't understand it well
enough, which leads to some minor/invisible effects (the .elc file is
marginally less efficient) and some less invisible effects (code after
that `let` will get spurious warnings (about a call to unknown function)
if it calls `fun`) :-(


        Stefan




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

* Re: curious why private variables are rare
  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:48 ` Emanuel Berg
  1 sibling, 0 replies; 5+ messages in thread
From: Emanuel Berg @ 2022-11-21 12:48 UTC (permalink / raw)
  To: help-gnu-emacs

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




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

* Re: curious why private variables are rare
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Emanuel Berg @ 2022-11-21 12:52 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor wrote:

> warnings [...] unknown function

Yes, but as you know, shut it up with `declare-function', e.g.

  (declare-function australian-survivor nil)

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: curious why private variables are rare
  2022-11-21 12:52   ` Emanuel Berg
@ 2023-10-02 23:56     ` Samuel Wales
  0 siblings, 0 replies; 5+ messages in thread
From: Samuel Wales @ 2023-10-02 23:56 UTC (permalink / raw)
  To: help-gnu-emacs

discoverd a possible bug in 27 with privte vriables.

in the following c-h f does not take you to the cl-defun.

(let (private-alist
      (unique-string
       (format "%s" (gensym "cleandiff-unify--xyzzy--"))))
  (define-key cleandiff-mode-map "U" 'cleandiff-unify)
  ;; this code works but possibly needs improving
  ;; c-h f fails to go to the correct location goes to bob
  (cl-defun cleandiff-unify
      ;; actually consider log 4
      (arg &aux (prefix (cond
                          ((= arg 1) 'usual)
                          ((= arg 4) 'extra)
                          ((= arg 16) 'clear)
                          ((= arg 64) 'update))))


On 11/21/22, Emanuel Berg <incal@dataswamp.org> wrote:
> Stefan Monnier via Users list for the GNU Emacs text editor wrote:
>
>> warnings [...] unknown function
>
> Yes, but as you know, shut it up with `declare-function', e.g.
>
>   (declare-function australian-survivor nil)
>
> --
> underground experts united
> https://dataswamp.org/~incal
>
>
>


-- 
The Kafka Pandemic

A blog about science, health, human rights, and misopathy:
https://thekafkapandemic.blogspot.com



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

end of thread, other threads:[~2023-10-02 23:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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).