all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Felix Dietrich <felix.dietrich@sperrhaken.name>
To: Emanuel Berg <incal@dataswamp.org>
Cc: help-gnu-emacs@gnu.org
Subject: Re: unused lexical variable in `cl-loop'
Date: Wed, 28 Sep 2022 14:40:47 +0200	[thread overview]
Message-ID: <87r0zv3c4w.fsf@sperrhaken.name> (raw)
In-Reply-To: <8735cclf95.fsf@dataswamp.org> (Emanuel Berg's message of "Tue, 27 Sep 2022 22:43:50 +0200")

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



  parent reply	other threads:[~2022-09-28 12:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-27 20:43 unused lexical variable in `cl-loop' Emanuel Berg
2022-09-28 10:58 ` Michael Heerdegen
2022-09-28 12:40 ` Felix Dietrich [this message]
2022-09-30 22:59   ` Emanuel Berg
2022-10-01 15:10     ` Stefan Monnier via Users list for the GNU Emacs text editor

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r0zv3c4w.fsf@sperrhaken.name \
    --to=felix.dietrich@sperrhaken.name \
    --cc=help-gnu-emacs@gnu.org \
    --cc=incal@dataswamp.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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.