all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* unused lexical variable in `cl-loop'
@ 2022-09-27 20:43 Emanuel Berg
  2022-09-28 10:58 ` Michael Heerdegen
  2022-09-28 12:40 ` Felix Dietrich
  0 siblings, 2 replies; 5+ messages in thread
From: Emanuel Berg @ 2022-09-27 20:43 UTC (permalink / raw)
  To: help-gnu-emacs

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

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/math.el

(require 'cl-lib)

(defun interval (&optional min max)
  (interactive "nmin: \nnmax: ")
  (or min (setq min 0))
  (or max (setq max 9))
  (unless (<= min max)
    (error "Bogus interval") )
  (cl-loop for i from min to max
           with num = ()
           collect i into num
           finally return num) )

;; (interval 10 5)
;; (interval)
;; (interval 19 99)

;; Warning: Unused lexical variable `num'
;; Warning: Unused lexical variable `num'

I don't know if that makes sense, it doesn't look like it,
however it gave me an idea:

(defun interval (&optional min max)
  (interactive "nmin: \nnmax: ")
  (or min (setq min 0))
  (or max (setq max 9))
  (unless (<= min max)
    (error "Bogus interval") )
  (cl-loop for i from min to max collect i) )

and indeed that works just as well and there isn't a warning.

So that's good ... but why nothing happens with 'num' in the
first attempt I wonder?

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




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

* Re: unused lexical variable in `cl-loop'
  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
  1 sibling, 0 replies; 5+ messages in thread
From: Michael Heerdegen @ 2022-09-28 10:58 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg <incal@dataswamp.org> writes:

>   (cl-loop for i from min to max
>            with num = ()
>            collect i into num
>            finally return num) )
>
> ;; Warning: Unused lexical variable `num'

Seems the "with num = ()" clause is redundant, `cl-loop' does that
implicitly.  Check with `macroexpand-1' with the `cl-loop' form.

Michael.




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

* Re: unused lexical variable in `cl-loop'
  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
  2022-09-30 22:59   ` Emanuel Berg
  1 sibling, 1 reply; 5+ messages in thread
From: Felix Dietrich @ 2022-09-28 12:40 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs

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



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

* Re: unused lexical variable in `cl-loop'
  2022-09-28 12:40 ` Felix Dietrich
@ 2022-09-30 22:59   ` Emanuel Berg
  2022-10-01 15:10     ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 5+ messages in thread
From: Emanuel Berg @ 2022-09-30 22:59 UTC (permalink / raw)
  To: help-gnu-emacs

Felix Dietrich wrote:

>   (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))
>
> The variable ‘num’ is bound twice

That's right, if you bind the same lexical/static variable
more than once you get the "Unused lexical variable" warning
for every occasion and it doesn't matter if it's used or not.

(let ((one 1)  
      (one 1) )
  one)

;; Warning: Unused lexical variable `one'
;; Warning: Unused lexical variable `one'

But if you do this ...

(let ((one 1)
      (one 2) )
  one)

You only get a warning for the first one ...

;; Warning: Unused lexical variable `one'

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




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

* Re: unused lexical variable in `cl-loop'
  2022-09-30 22:59   ` Emanuel Berg
@ 2022-10-01 15:10     ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-10-01 15:10 UTC (permalink / raw)
  To: help-gnu-emacs

> (let ((one 1)  
>       (one 1) )
>   one)
>
> ;; Warning: Unused lexical variable `one'
> ;; Warning: Unused lexical variable `one'
>
> But if you do this ...
>
> (let ((one 1)
>       (one 2) )
>   one)
>
> You only get a warning for the first one ...

Ooh, that's a good one, thanks.  Sounds like a bug in `cconv.el` somewhere.


        Stefan




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

end of thread, other threads:[~2022-10-01 15:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2022-09-30 22:59   ` Emanuel Berg
2022-10-01 15:10     ` Stefan Monnier via Users list for the GNU Emacs text editor

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.