From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Re: How to avoid compiler warning `unused lexical variable' for `dolist' or `dotimes'? Date: Fri, 8 Jan 2021 08:40:08 +0300 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34121"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/2.0 (3d08634) (2020-11-07) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jan 08 06:44:21 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kxkZQ-0008mT-Vn for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 08 Jan 2021 06:44:20 +0100 Original-Received: from localhost ([::1]:50652 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kxkZP-0000CZ-VJ for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 08 Jan 2021 00:44:19 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35216) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kxkYd-0000B7-Sb for help-gnu-emacs@gnu.org; Fri, 08 Jan 2021 00:43:32 -0500 Original-Received: from stw1.rcdrun.com ([217.170.207.13]:34343) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kxkYb-00074Y-Bq for help-gnu-emacs@gnu.org; Fri, 08 Jan 2021 00:43:31 -0500 Original-Received: from localhost ([::ffff:41.210.145.49]) (AUTH: PLAIN securesender, TLS: TLS1.2,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 0000000000296BE3.000000005FF7F0FE.00005DA0; Thu, 07 Jan 2021 22:43:25 -0700 Mail-Followup-To: help-gnu-emacs@gnu.org Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=217.170.207.13; envelope-from=bugs@gnu.support; helo=stw1.rcdrun.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:127093 Archived-At: * Stefan Monnier [2021-01-08 08:14]: > > To me that means that I need to use workaround because `dotimes' does > > not work well as described in docstring. I need to remember something > > that is nowhere documented. > > In which way doesn't it work as documented? > The warning is just that: a warning. It doesn't affect the actual behavior. > > > Either compiler warning is wrong, or docstring is wrong. One of those > > shall be improved. > > AFAIK both are right: you put into the 3rd arg an expression that is > evaluated in a context where a new variable `var` has been added for you > but you don't make use of that variable. This fact doesn't prevent the > code from being valid and executed correctly, but it is suspicious so it > deserves a warning. I think there is some mistake here. I have placed third value where I use the third value, but compiler warning is complaining about first one. For example in (dolist (n 10 return) ...) compiler would warn about `n' and not about `return'. If it complains about `return' when not used, that would be just fine for me, good warning and logical. But I do not speak of that case. Complete example, in file new.el: ;; -*- lexical-binding: t; -*- (defun my-fun () (let ((list '())) (dotimes (n 10 list) (push n list)))) Compiling file /home/data1/protected/new.el at Fri Jan 8 08:34:04 2021 new.el:3:1: Warning: Unused lexical variable n So that is what I speak about. The `n' variable is assigned right there similar to `let' so I think that maybe macro shall be improved. > A warning is emitted when the code is valid but where we think it's > useful to bring the attention to something of which the programmer may > not be aware. I wish to make all programs warning free. If I need to get aware of some function than such warning should be placed in the function documentation. For example, what I was thinking first is that `n' from above example should be somehow already declared. The docstring of `dotimes' should tell me something about that. But we have here hidden anti-features that are not documented, known only to developers and available only in mailing list messages. Sorry I cannot understand the macro expansion at this moment, but thank you. Maybe somebody else understands it. > We occasionally also use those warnings to nudge programmers toward > a particular programming style, i.e. with a conscious decision to make > people change their style, which makes it yet more likely that some > people will disagree (at least at first, until they get used to the new > style). E.g. in early Emacs it was a lot more common to find packages > which relied on non-trivial ways uses of dynamic scoping and undeclared > global vars. After adding warnings for uses of "free variables" the > style changed over the years, making it possible in Emacs-24 to > introduce lexical scoping such that the vast majority of the code works > unchanged when `lexical-binding` is activated. That is great. Only it is not relevant to macro or function that is documented to work, but then again it gives warnings for `n' and not for `return' as in above example. I am surprised. For me personally I will stick to the built-in functions such as `while' and mapping functions and will rather try to avoid `dotimes' and `dolist', unless the error is corrected. Jean