Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following elisp code inserts hello world! 10 times:

-*- lexical-binding: t -*-
(dotimes (i 10) (insert "hello world!\n"))

If I byte-compile that code, I get the following message:

hellos.el:2:1:Warning: Unused lexical variable `i'

This is strange ... i is used to count from 0 to 9, even if it is not used in the repeated code. How am I supposed to loop 10 times ?!

Even stranger, the warning message disappears if I set

-*- lexical-binding: nil -*-

Go figure ...

Is this a bug (admittedly minor) in the emacs compiler? Is there a way to loop 10 times without using a counting variable?

share|improve this question
up vote 10 down vote accepted

You don't use i and that's what the byte-compiler tells you. To let the byte-compiler know that you're aware of the fact that the variable is unused, give it a name that starts with an underscore. E.g.

(dotimes (_ 10) ...)

or

(dotimes (_i 10) ...)
share|improve this answer
    
Thanks !! BTW, why is the byte-compiler not complaining when lexical-binding is not set? – phs Feb 19 at 17:33
5  
Because with dynamic scoping the byte compiler can't be sure that the variable is really not used (e.g. the insert function in your example could theoretically make use of it). – Stefan Feb 19 at 17:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.