all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Idiomatic way to avoid unused lexical variable in ‘dotimes’ or ‘dolist’?
@ 2024-03-26 22:26 tpeplt
  2024-03-26 22:30 ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 5+ messages in thread
From: tpeplt @ 2024-03-26 22:26 UTC (permalink / raw)
  To: help-gnu-emacs

Consider a file that contains the following lines only:

┌───────────────────────────────
│;; -*- lexical-binding: t; -*-
│
│(dotimes (i 100)
│  (insert "I will not obey absurd orders\n"))
└───────────────────────────────

(Example from the Emacs Lisp manual.)

When this file is byte-compiled, the compiler will issue a warning:

> Warning: Unused lexical variable `i'

The following change could be used to eliminate this warning:

(dotimes (i 100)
  (null i)
  (insert "I will not obey absurd orders\n"))

1. Is there an idiom in Emacs Lisp for writing this that
   eliminates this warning?

2. Should the compiler be changed so that it does not issue this warning
   for ‘dotimes’ and ‘dolist’, where declaring the variable required,
   but use of the variable is optional?

--

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

* Re: Idiomatic way to avoid unused lexical variable in ‘dotimes’ or ‘dolist’?
  2024-03-26 22:26 Idiomatic way to avoid unused lexical variable in ‘dotimes’ or ‘dolist’? tpeplt
@ 2024-03-26 22:30 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2024-03-27  2:02   ` tpeplt
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2024-03-26 22:30 UTC (permalink / raw)
  To: help-gnu-emacs

> 1. Is there an idiom in Emacs Lisp for writing this that
>    eliminates this warning?

    (dotimes (_ 100)
      (insert "I will not obey absurd orders\n"))

Or any other var name that starts with an underscore.


        Stefan




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

* Re: Idiomatic way to avoid unused lexical variable in ‘dotimes’ or ‘dolist’?
  2024-03-26 22:30 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2024-03-27  2:02   ` tpeplt
  2024-03-27  3:52     ` Tim Landscheidt
  0 siblings, 1 reply; 5+ messages in thread
From: tpeplt @ 2024-03-27  2:02 UTC (permalink / raw)
  To: Stefan Monnier via Users list for the GNU Emacs text editor
  Cc: Stefan Monnier

Stefan Monnier via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

>> 1. Is there an idiom in Emacs Lisp for writing this that
>>    eliminates this warning?
>
>     (dotimes (_ 100)
>       (insert "I will not obey absurd orders\n"))
>
> Or any other var name that starts with an underscore.
>
>
>         Stefan

Thank you.  I have not been able to find this documented anywhere (that
is, that lexical variables whose names begin with an underscore are not
flagged with a warning message if they are not referenced).  This
appears to be true with, for example, ‘let’ expressions, also.

--



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

* Re: Idiomatic way to avoid unused lexical variable in ‘dotimes’ or ‘dolist’?
  2024-03-27  2:02   ` tpeplt
@ 2024-03-27  3:52     ` Tim Landscheidt
  2024-03-27  5:01       ` tpeplt
  0 siblings, 1 reply; 5+ messages in thread
From: Tim Landscheidt @ 2024-03-27  3:52 UTC (permalink / raw)
  To: tpeplt; +Cc: help-gnu-emacs, Stefan Monnier

(anonymous) wrote:

>>> 1. Is there an idiom in Emacs Lisp for writing this that
>>>    eliminates this warning?

>>     (dotimes (_ 100)
>>       (insert "I will not obey absurd orders\n"))

>> Or any other var name that starts with an underscore.

> Thank you.  I have not been able to find this documented anywhere (that
> is, that lexical variables whose names begin with an underscore are not
> flagged with a warning message if they are not referenced).  This
> appears to be true with, for example, ‘let’ expressions, also.

It is mentioned in the GNU Emacs Lisp Reference Manual (C-h
i g (elisp) RET) in the node "Converting to Lexical
Binding":

| […]

|    A warning about an unused variable may be a good hint that the
| variable was intended to be dynamically scoped (because it is actually
| used, but in another function), but it may also be an indication that
| the variable is simply really not used and could simply be removed.  So
| you need to find out which case it is, and based on that, either add a
| ‘defvar’ or remove the variable altogether.  If removal is not possible
| or not desirable (typically because it is a formal argument and that we
| cannot or don’t want to change all the callers), you can also add a
| leading underscore to the variable’s name to indicate to the compiler
| that this is a variable known not to be used.)

| […]

Tim



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

* Re: Idiomatic way to avoid unused lexical variable in ‘dotimes’ or ‘dolist’?
  2024-03-27  3:52     ` Tim Landscheidt
@ 2024-03-27  5:01       ` tpeplt
  0 siblings, 0 replies; 5+ messages in thread
From: tpeplt @ 2024-03-27  5:01 UTC (permalink / raw)
  To: Tim Landscheidt; +Cc: help-gnu-emacs, Stefan Monnier

Tim Landscheidt <tim@tim-landscheidt.de> writes:

>
>>>> 1. Is there an idiom in Emacs Lisp for writing this that
>>>>    eliminates this warning?
>
>>>     (dotimes (_ 100)
>>>       (insert "I will not obey absurd orders\n"))
>
>>> Or any other var name that starts with an underscore.
>
>> Thank you.  I have not been able to find this documented anywhere (that
>> is, that lexical variables whose names begin with an underscore are not
>> flagged with a warning message if they are not referenced).  This
>> appears to be true with, for example, ‘let’ expressions, also.
>
> It is mentioned in the GNU Emacs Lisp Reference Manual (C-h
> i g (elisp) RET) in the node "Converting to Lexical
> Binding":
>
> | […]
>
> |    A warning about an unused variable may be a good hint that the
> | variable was intended to be dynamically scoped (because it is actually
> | used, but in another function), but it may also be an indication that
> | the variable is simply really not used and could simply be removed.  So
> | you need to find out which case it is, and based on that, either add a
> | ‘defvar’ or remove the variable altogether.  If removal is not possible
> | or not desirable (typically because it is a formal argument and that we
> | cannot or don’t want to change all the callers), you can also add a
> | leading underscore to the variable’s name to indicate to the compiler
> | that this is a variable known not to be used.)
>
> | […]
>

Again, thank you.  This will be useful knowledge in future instances of
variables that have to be declared, but are not referenced.

--




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

end of thread, other threads:[~2024-03-27  5:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-26 22:26 Idiomatic way to avoid unused lexical variable in ‘dotimes’ or ‘dolist’? tpeplt
2024-03-26 22:30 ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-03-27  2:02   ` tpeplt
2024-03-27  3:52     ` Tim Landscheidt
2024-03-27  5:01       ` tpeplt

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.