From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuri Khan Newsgroups: gmane.emacs.help Subject: Re: Example use of macro to minimize and generalize the code Date: Wed, 2 Jun 2021 00:57:36 +0700 Message-ID: References: <87y2buscef.fsf@web.de> <87k0nes9s0.fsf@web.de> <878s3us8le.fsf@web.de> <87fsy1y97e.fsf@web.de> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="15296"; mail-complaints-to="usenet@ciao.gmane.io" To: Yuri Khan , Michael Heerdegen , help-gnu-emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jun 01 19:58:13 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 1lo8eb-0003jO-Lv for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 01 Jun 2021 19:58:13 +0200 Original-Received: from localhost ([::1]:51378 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lo8ea-0001fv-OV for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 01 Jun 2021 13:58:12 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:39932) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lo8eE-0001by-Ge for help-gnu-emacs@gnu.org; Tue, 01 Jun 2021 13:57:50 -0400 Original-Received: from mail-ua1-x936.google.com ([2607:f8b0:4864:20::936]:46684) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lo8eC-0005K8-It for help-gnu-emacs@gnu.org; Tue, 01 Jun 2021 13:57:50 -0400 Original-Received: by mail-ua1-x936.google.com with SMTP id p1so8914512uam.13 for ; Tue, 01 Jun 2021 10:57:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=xUq5LKlWTcGzC5wS9ILrs70gVm0yl7dFvDwTq/3z0VI=; b=Dgxj56xfwS8vzj3+Pptzs84os/MvxC99WrAfxGi2+BvL47q8kjUSlCoEM3dRnmfkau DlSkFt6UJRZfMDhu4UX8k7M3cWnaBoD3/WWVFJcyXch4emvv7FRK3aR4rtaYkClZ82fF UihTUFk+SEG6qvgk8ChbuQjL4jPoYM5VriPHjmhin1u70p8YXiJa0QJ5W2jshP3vyXHd WcI+VZcyFV8OXliStnr5yArHnQIWjyuVh4uPEi85v1/i0TgulcTULxZpbhzrnxYBQNJq w7Q7YaBDNitwOxmz7gfXG+hT0upkctzyUqpmbmsJAX61gQhyQoV7hJ2tBI71tsjz4cC9 EoCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=xUq5LKlWTcGzC5wS9ILrs70gVm0yl7dFvDwTq/3z0VI=; b=uoJsNNgh5u/7KkemD2vhqeINJoaf/by7W2euM950pLjBEri6z3vzpYYhU4bZnMatCO 9l3+Cwk2O8EKl1leKavxmIVdm5zVJq8gV+VoxP8LjQyrkX8EXUx0vRevgkhJ2PJ7cd/1 1enpwEeb4Kbu1GvLUuG8XVNrFLXFpT+P1n12owpsbR60njQlqDhq6gAvaa+zD/5of7mg lD6v7hTjHMK8TgdN8VCEaa53xh97dIRndGN088TgDBna0n9vIc4NQcKN8Pf/9vCTGakm yyjoyTOJK6H2V0ViIHFiBEoFDx+V9nMdd9J3jWyAEDiElM3jHuwNVY+ghTfZjyTUnNVd d72g== X-Gm-Message-State: AOAM5305O082XdiqLTyfujyyON/caVWIpY9FRkhzKNDNq+EMONEGTwW4 S6MRV0eLYUocacbzKfh3/ARm5Mx62SoUtLiTMxs= X-Google-Smtp-Source: ABdhPJyP9cB7hAgRXt871cKCAMwlpTLTzKDx9mNCWdVZEREp6C4/pDVD4uVzrxeFATT77DKpU6Z105MivSU9yAg36NA= X-Received: by 2002:ab0:2b15:: with SMTP id e21mr17062875uar.102.1622570267612; Tue, 01 Jun 2021 10:57:47 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::936; envelope-from=yurivkhan@gmail.com; helo=mail-ua1-x936.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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:130465 Archived-At: On Wed, 2 Jun 2021 at 00:27, Jean Louis wrote: > I have removed `id' argument and now I have: > > (macroexpand '(when-tabulated-id "people" (ignore))) > =E2=87=92 (if id (progn (ignore)) > (if (or (eq "people" 'any) (string-equal "people" rcd-current-table)) > (let ((id (tabulated-list-get-id))) > (if id (progn (ignore)) (message "Did not get ID"))) > (message "This function is for table `%s' only" "people"))) > > If I would use ,d it would expand into number or nil, it seem it would en= d up same. > > Now I don't have compiler warnings, and functions work fine. > > Let me know if I am making errors in this. Yes you are. Your macro expands to a form that refers to a symbol named =E2=80=9Cid=E2=80=9D from outer scope. Next, there are several possib= ilities: * The symbol is not defined, in which case the result of expansion will signal (void-variable id): (when-tabulated-id "people" (ignore)) =E2=87=92 Debugger entered--Lisp error: (void-variable id) * The user of your macro has to define a variable named =E2=80=9Cid=E2=80= =9D globally or buffer-locally. This is bad because the name =E2=80=9Cid=E2=80=9D is not namespaced. In your own package, any variable names you define should start with a prefix unique to your package. (setq id t) (when-tabulated-id "people" (ignore)) =E2=87=92 nil * The user has to use your macro in a context where =E2=80=9Cid=E2=80=9D is= bound as a local variable or a function argument. If that=E2=80=99s your intent, you should document it in the docstring of the macro, but it=E2=80=99s fragile because nobody expects forms to behave this way. (let ((id t)) (when-tabulated-id "people" (ignore))) =E2=87=92 nil (defun foo (id) (when-tabulated-id "people" (ignore))) (foo t) =E2=87=92 nil * Lastly, you might have intended this macro to be used only by you. In that case, it should be namespaced to your package with a double hyphen after the prefix. Anyway, three month later, you forget that the macro depends on =E2=80=9Cid=E2=80=9D and you stare at your (let ((id t= )) =E2=80=A6) or (defun foo (id) =E2=80=A6) which doesn=E2=80=99t obviously use =E2=80=9Cid= =E2=80=9D and remove the binding. Suddenly everything breaks.