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

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

Re: unused lexical variable in `cl-loop'


From: Felix Dietrich
Subject: Re: unused lexical variable in `cl-loop'
Date: Wed, 28 Sep 2022 14:40:47 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux)

Hello Emanuel,

Emanuel Berg <incal@dataswamp.org> writes:

> This code works but the byte-compiler says the lexical
> variable 'num' is unused on the first two occasions it occurs
> in the code.

>   (cl-loop for i from min to max
>            with num = ()
>            collect i into num
>            finally return num) )

> ;; Warning: Unused lexical variable `num'

Using the command ‘emacs-lisp-macroexpand’ on the above form (with the
cursor on the first opening parentheses before ‘cl-loop’) yields:

#+begin_src emacs-lisp
  (cl-block nil
    (let*
        ((i min)
         (--cl-var-- max)
         (num nil)
         (num nil))
      (while
          (<= i --cl-var--)
        (setq num
              (nconc num
                     (list i)))
        (setq i
              (+ i 1)))
      num))
#+end_src

The variable ‘num’ is bound twice.  I suspect that the “with num = ()”
is redundant, but do not know how “collect into” is actually handled in
the ‘cl-loop’ macro and how it binds the receiving variable.  You will
have to read the macroʼs implementation yourself to figure that out.
Here is what the info page has to say about “into VAR” [1]:

#+begin_quote
Accumulation clauses can be followed by ‘into VAR’ to cause the data
to be collected into variable VAR (which is automatically ‘let’-bound
during the loop) rather than an unnamed temporary variable.
#+end_quote


Footnotes:
[1]  (info "(cl) Accumulation Clauses")

-- 
Felix Dietrich



reply via email to

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