unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Noam Postavsky <npostavs@gmail.com>
To: Michael Heerdegen <michael_heerdegen@web.de>
Cc: 18059@debbugs.gnu.org, Stefan Monnier <monnier@IRO.UMontreal.CA>
Subject: bug#18059: 24.3.92; defvar and special variables
Date: Sun, 04 Mar 2018 18:27:54 -0500	[thread overview]
Message-ID: <878tb72zgl.fsf@gmail.com> (raw)
In-Reply-To: <874lm7u554.fsf@web.de> (Michael Heerdegen's message of "Fri, 23 Feb 2018 15:55:51 +0100")

[-- Attachment #1: Type: text/plain, Size: 661 bytes --]

Michael Heerdegen <michael_heerdegen@web.de> writes:

> Stefan Monnier <monnier@IRO.UMontreal.CA> writes:
>
>> This `defvar` case was devised specifically as a way to make it
>> possible to provide something like fluid-let without having to
>> introduce a new special form.
>
> Then we should document that use case.

Yes, you're right.  Since this case really relates more to the lexical
vs dynamic binding, I think it would make more sense to add the example
in a later section (with a link, of course).  I still don't think I can
claim with a straight face that (let (_) (defvar foo) ...) being
different than (let () (defvar foo) ...) is not a bug though.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 3601 bytes --]

From 3e62f1d6d892b26993ad5553ee17a2aaeae67d8b Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs@gmail.com>
Date: Sat, 10 Feb 2018 14:06:05 -0500
Subject: [PATCH v3] Explain more about (defvar foo) form (Bug#18059)

* doc/lispref/variables.texi (Defining Variables)
(Using Lexical Binding):
* doc/lispref/compile.texi (Compiler Errors): Emphasize that omitting
VALUE for `defvar' marks the variable special only locally.
---
 doc/lispref/compile.texi   |  5 +++--
 doc/lispref/variables.texi | 38 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/doc/lispref/compile.texi b/doc/lispref/compile.texi
index 0e39866d34..70da9727ee 100644
--- a/doc/lispref/compile.texi
+++ b/doc/lispref/compile.texi
@@ -500,8 +500,9 @@ Compiler Errors
 @item
 Likewise, you can tell the compiler that a variable is defined using
 @code{defvar} with no initial value.  (Note that this marks the
-variable as special, i.e.@: dynamically bound.)  @xref{Defining
-Variables}.
+variable as special, i.e.@: dynamically bound, but only for the rest
+of the current binding form, or file if at top-level.)
+@xref{Defining Variables}.
 @end itemize
 
   You can also suppress any and all compiler warnings within a certain
diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi
index e025d3fd10..9acdb210e1 100644
--- a/doc/lispref/variables.texi
+++ b/doc/lispref/variables.texi
@@ -442,8 +442,13 @@ Defining Variables
 evaluated and @var{symbol} is set to the result.  But if @var{symbol}
 is not void, @var{value} is not evaluated, and @var{symbol}'s value is
 left unchanged.  If @var{value} is omitted, the value of @var{symbol}
-is not changed in any case.  Using @code{defvar} with no value is one
-method of suppressing byte compilation warnings, see @ref{Compiler
+is not changed in any case.
+
+Note that specifying a value, even @code{nil}, marks the variable as
+special permanently.  Whereas if @var{value} is omitted then the
+variable is only marked special locally (i.e.@: for the rest of the
+current binding form, or file if at the top-level).  This can be
+useful for suppressing byte compilation warnings, see @ref{Compiler
 Errors}.
 
 If @var{symbol} has a buffer-local binding in the current buffer,
@@ -488,6 +493,9 @@ Defining Variables
 
 The @code{defvar} form returns @var{symbol}, but it is normally used
 at top level in a file where its value does not matter.
+
+For a more elaborate example of using @code{defvar} without a value,
+@xref{Local defvar example}.
 @end defspec
 
 @cindex constant variables
@@ -1164,6 +1172,32 @@ Using Lexical Binding
 (@pxref{Defining Variables}).  All other variables are subject to
 lexical binding.
 
+@anchor{Local defvar example}
+Using @code{defvar} without a value, it is possible to bind a variable
+dynamically just in one file, or in just one part of a file while
+still binding it lexically elsewhere.  For example:
+
+@example
+@group
+(let (_)
+  (defvar x)      ; @r{Let-bindings of @code{x} will be dynamic within this let.}
+  (let ((x -99))  ; @r{This is a dynamic binding of @code{x}.}
+    (defun get-dynamic-x ()
+      x)))
+
+(let ((x 'lexical)) ; @r{This is a lexical binding of @code{x}.}
+  (defun get-lexical-x ()
+    x))
+
+(let (_)
+  (defvar x)
+  (let ((x 'dynamic))
+    (list (get-lexical-x)
+          (get-dynamic-x))))
+    @result{} (lexical dynamic)
+@end group
+@end example
+
 @defun special-variable-p symbol
 This function returns non-@code{nil} if @var{symbol} is a special
 variable (i.e., it has a @code{defvar}, @code{defcustom}, or
-- 
2.11.0


  reply	other threads:[~2018-03-04 23:27 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-20  3:23 bug#18059: 24.3.92; defvar and special variables Michael Heerdegen
2018-02-10 19:29 ` Noam Postavsky
2018-02-10 23:59   ` Michael Heerdegen
2018-02-11  0:17     ` Drew Adams
2018-02-11  0:43       ` Noam Postavsky
2018-02-11  0:38     ` Noam Postavsky
2018-02-11  1:32       ` Michael Heerdegen
2018-02-11  2:26         ` Noam Postavsky
2018-02-11 15:14       ` Stefan Monnier
2018-02-11 15:35         ` Noam Postavsky
2018-02-11 16:38           ` Michael Heerdegen
2018-02-11 17:00             ` Noam Postavsky
2018-02-14  0:27           ` Noam Postavsky
2018-02-18 22:28             ` Noam Postavsky
2018-02-19  1:44               ` Michael Heerdegen
2018-02-21 15:09                 ` Noam Postavsky
2018-02-21 16:08                   ` Drew Adams
2018-02-21 16:23                     ` Noam Postavsky
2018-02-21 16:33                       ` Noam Postavsky
2018-02-21 17:15                         ` Drew Adams
2018-02-21 18:00                           ` Noam Postavsky
2018-02-21 19:14                             ` Drew Adams
2018-02-21 19:19                             ` Stefan Monnier
2018-02-21 22:20                               ` Michael Heerdegen
2018-02-23 14:05                                 ` Stefan Monnier
2018-02-23 14:55                                   ` Michael Heerdegen
2018-03-04 23:27                                     ` Noam Postavsky [this message]
2018-03-05  9:51                                       ` Michael Heerdegen
2018-03-07 13:00                                         ` Noam Postavsky
2018-03-09  9:58                                           ` Michael Heerdegen
2018-03-09 13:43                                             ` Stefan Monnier
2018-03-11 16:45                                               ` Noam Postavsky
2018-03-11 22:16                                                 ` Drew Adams
2018-03-14 11:15                                                   ` Noam Postavsky
2018-03-23 12:23                                                     ` Noam Postavsky
2018-03-05 15:57                                       ` Eli Zaretskii
2018-03-08 23:36                             ` Noam Postavsky
2018-03-09  0:15                               ` Drew Adams
2018-02-21 17:11                       ` Drew Adams
2018-02-11 15:57     ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=878tb72zgl.fsf@gmail.com \
    --to=npostavs@gmail.com \
    --cc=18059@debbugs.gnu.org \
    --cc=michael_heerdegen@web.de \
    --cc=monnier@IRO.UMontreal.CA \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).