From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Drew Adams Newsgroups: gmane.emacs.help Subject: RE: To `boundp' or not to `boundp'? Date: Tue, 1 Sep 2015 09:42:31 -0700 (PDT) Message-ID: <933687a5-d288-499a-a34a-93260456d907@default> References: <55E5C99B.3020608@yandex.ru> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1441125786 15463 80.91.229.3 (1 Sep 2015 16:43:06 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 1 Sep 2015 16:43:06 +0000 (UTC) To: Dmitry Gutov , Alexander Shukaev , help-gnu-emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Sep 01 18:42:54 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZWoe5-0005H4-Aq for geh-help-gnu-emacs@m.gmane.org; Tue, 01 Sep 2015 18:42:53 +0200 Original-Received: from localhost ([::1]:55894 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWoe5-00030G-0W for geh-help-gnu-emacs@m.gmane.org; Tue, 01 Sep 2015 12:42:53 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:44375) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWodu-0002kY-Eu for help-gnu-emacs@gnu.org; Tue, 01 Sep 2015 12:42:43 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZWodq-0004tM-Ut for help-gnu-emacs@gnu.org; Tue, 01 Sep 2015 12:42:42 -0400 Original-Received: from userp1040.oracle.com ([156.151.31.81]:20907) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWodq-0004tF-P3 for help-gnu-emacs@gnu.org; Tue, 01 Sep 2015 12:42:38 -0400 Original-Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id t81GgWEd012851 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 1 Sep 2015 16:42:33 GMT Original-Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by userv0022.oracle.com (8.13.8/8.13.8) with ESMTP id t81GgWH0003579 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Tue, 1 Sep 2015 16:42:32 GMT Original-Received: from abhmp0012.oracle.com (abhmp0012.oracle.com [141.146.116.18]) by aserv0121.oracle.com (8.13.8/8.13.8) with ESMTP id t81GgWrs023958; Tue, 1 Sep 2015 16:42:32 GMT In-Reply-To: <55E5C99B.3020608@yandex.ru> X-Priority: 3 X-Mailer: Oracle Beehive Extensions for Outlook 2.0.1.9 (901082) [OL 12.0.6691.5000 (x86)] X-Source-IP: userv0022.oracle.com [156.151.31.74] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 156.151.31.81 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 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.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:106963 Archived-At: > > I often see code like this > > (when (and (boundp 'xxx-mode) xxx-mode) ...) >=20 > The "proper" way to do that is to use bound-and-true-p. That certainly is *not* proper for the more general case (and (boundp 'xxx) xxx), where the value of `xxx' is not necessarily Boolean or is not used only as a Boolean. Well, it works the same, but the _name misleads_ terribly in this case, even if the doc does let you know that the variable value (not t or nil) is returned, when bound. `bound-and-true-p' is defined as the exact _same thing_: (defmacro bound-and-true-p (var) "Return the value of symbol VAR if it is bound, else nil." `(and (boundp (quote ,var)) ,var)) So it cannot be more "proper". And in fact its definition (expansion) is much, much clearer in the case where the var value is not Boolean. Unlike (bound-and-true-p VAR), (and (boundp 'VAR) VAR) is always clear and never misleads. It makes clear that you are testing the symbol for variableness and returning the variable value if bound. In sum, your "proper" is my "improper, misleading, and useless." Why Emacs bothered to introduce this silly macro, I cannot imagine. Perhaps it was to save some=20 characters when the variable name is long. Bof. If they really wanted a macro for this, it should have been called something that reflects what it does, e.g., `var-value-or-nil' or `if-bound-then-value-else-nil'. > > I personally prefer to do it shorter > > (when (ignore-errors xxx-mode) ...) > > Are those equivalent? Yes, pretty much. > Are there any pitfalls associated with my approach? Not really. But it is not as readable, IMHO. `boundp' shouts to a reader that this is test for a variable.