help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Appending to a list


From: Emanuel Berg
Subject: Re: Appending to a list
Date: Mon, 14 Dec 2020 05:50:47 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

steve-humphreys wrote:

> I thought I could push element by element.

You can, you can push whatever, if that data structure makes it
more involved solve it for onee pilot case, then write a small
`defun', put it there, and ever after just pass the single
element to that.

> I got inspiration from some discussions here that push is
> more efficient and wanted to have a go. Yes, I do use push to
> make the larger list. Have not thought about defvar before.
> I have read that "setq" does not introduce a variable, but
> I would need some explanation about that.

It works with `setq' only without `defvar' the byte-compiler,
which should always be used BTW, without it the byte-compiler
will complain.

Comment-out the defvars in this

(require 'cl-lib)

(defvar bird-list-all)
(defvar bird-list-1)
(defvar bird-list-2)

(setq bird-list-all '())

(setq bird-list-1   '("Western jackdaw" "European goldfinch"))
(setq bird-list-2   '("rook"))

(cl-pushnew bird-list-1 bird-list-all) ; (("Western jackdaw" "European 
goldfinch"))
(cl-pushnew bird-list-2 bird-list-all) ; (("rook") ("Western jackdaw" "European 
goldfinch"))

and the byte-compiler will say

  geh.el: 
  In toplevel form:
  geh.el:193:7: Warning: assignment to free variable ‘bird-list-all’
  geh.el:195:7: Warning: assignment to free variable ‘bird-list-1’
  geh.el:196:7: Warning: assignment to free variable ‘bird-list-2’
  geh.el:198:13: Warning: reference to free variable ‘bird-list-1’
  geh.el:198:25: Warning: reference to free variable ‘bird-list-all’
  geh.el:199:13: Warning: reference to free variable ‘bird-list-2’

In more ambitious projects, like a package, e.g. [1], defvar
should be used also to make available documentation, both in
the code and for the on-line help which in practice is the same
thing - Emacs is self-documenting, remember?

Heh, not really, right? But still, do it.

Anyway, don't rely too much on variables, defvar or no defvar.
Better to rely on defuns and use `let' and `let*' there. IMO!

[1] https://dataswamp.org/~incal/emacs-init/buc.el

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]