unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Real-life examples of lexical binding in Emacs Lisp
Date: Wed, 17 Jun 2015 12:49:47 +0200	[thread overview]
Message-ID: <871thatxro.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.5136.1434522217.904.help-gnu-emacs@gnu.org

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> Am 17.06.2015 um 02:06 schrieb Emanuel Berg:
>> Jim Diamond <Jim.Diamond@deletethis.AcadiaU.ca>
>> writes:
>>
>>> Really? Are there well-agreed-upon studies showing
>>> those things? Or are they your opinion?
>>>
>>> It strikes me that lexical scoping is easier to
>>> implement for compiled languages (that is an "off
>>> the cuff" comment from someone (me) with basic
>>> knowledge of compiler construction). But if lexical
>>> scoping is "more natural", is that because more
>>> people were "brought up" with lexically-scoped
>>> languages than dynamically-scoped languages?
>> This discussion is much easier to have if that
>> confusing terminology is dropped for a second and we
>> instead study the simple example of a `let' form:
>>
>>      (let ((scratch-buffer "*scratch*"))
>>        (when (bufferp scratch-buffer)
>>          (kill-buffer scratch-buffer) ))
>>
>> Here we have one piece of data which is used twice, so
>> that data is named and when it is used it is
>> indirectly refered to.
>>
>> In this example, what is natural to me? Answer:
>> I don't expect `let' to affect any other code than the
>> code in the `let' itself! And this is "lexical
>> scoping".
>
> Nonetheless, that's the way Emacs acted all the time, while called
> "dynamically" scoped.
>
> Now with "lexical" we have instead an injection, if a function with
> same arguments' symbol is called inside let.
>
> Seems neither "lexical" nor "dynamic" express the real thing.

To be more concrete, here is a case where something wrong happens:

     (setf lexical-binding nil)

     (defun do-something (arg) (format "\n%S\n" arg))

     (defun some-function (arg)
        (setf scratch-buffer (get-buffer-create " *some-function scratch buffer*"))
        (with-current-buffer scratch-buffer (insert (do-something arg))))

     (defun some-other-function ()
        (with-current-buffer scratch-buffer
           (buffer-substring (point-min) (point-max))))

     ;; and then in some unrelated code in a different file:

     (setq lexical-binding nil)
     (let ((scratch-buffer (get-buffer-create "*scratch*")))
        (with-current-buffer scratch-buffer (insert "hello"))
        (some-function "Howdy?")
        (with-current-buffer scratch-buffer
          (buffer-substring (point-min) (point-max))))
    -->
    "
    \"Howdy?\"

    \"Howdy?\"
    "

    ; instead of "hello" !!!

On the other hand, if you use lexical binding:

     (setq lexical-binding t)
     (let ((scratch-buffer (get-buffer-create "*scratch*")))
        (with-current-buffer scratch-buffer (insert "hello"))
        (some-function "Howdy?")
        (with-current-buffer scratch-buffer
          (buffer-substring-no-properties (point-min) (point-max))))
    -->
    ";; This buffer is for notes you don't want to save, and for Lisp evaluation.
    ;; If you want to create a file, visit that file with C-x C-f,
    ;; then enter the text in that file's own buffer.
    hello"

then this independent code stays independent and clean, and no other
function may fuck it.

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


  parent reply	other threads:[~2015-06-17 10:49 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-29  8:28 Real-life examples of lexical binding in Emacs Lisp Marcin Borkowski
2015-05-30  8:28 ` Tassilo Horn
2015-06-14 10:52   ` Marcin Borkowski
     [not found]   ` <mailman.4976.1434279182.904.help-gnu-emacs@gnu.org>
2015-06-14 11:31     ` Pascal J. Bourguignon
2015-06-16 23:48       ` Jim Diamond
2015-06-17  0:06         ` Emanuel Berg
2015-06-17  6:23           ` Andreas Röhler
     [not found]           ` <mailman.5136.1434522217.904.help-gnu-emacs@gnu.org>
2015-06-17 10:49             ` Pascal J. Bourguignon [this message]
2015-06-17 10:53               ` Pascal J. Bourguignon
2015-06-17 14:42                 ` Stefan Monnier
2015-06-17 16:19                   ` Andreas Röhler
2015-06-17 19:30                     ` Tassilo Horn
     [not found]                   ` <mailman.5171.1434557990.904.help-gnu-emacs@gnu.org>
2015-06-17 17:12                     ` Stefan Monnier
2015-06-17 20:22                   ` Emanuel Berg
2015-06-17 22:13                     ` Pascal J. Bourguignon
2015-06-17 23:46                       ` Emanuel Berg
2015-06-18 14:57                     ` Udyant Wig
2015-06-18 15:47                       ` Emanuel Berg
2015-06-19 13:49                         ` Udyant Wig
2015-06-19 17:41                           ` acronymania (was: Re: Real-life examples of lexical binding in Emacs Lisp) Emanuel Berg
2015-06-19 17:53                             ` Rusi
2015-06-17 20:33             ` Real-life examples of lexical binding in Emacs Lisp Emanuel Berg
2015-06-17 22:07               ` Robert Thorpe
2015-06-17 22:17                 ` Pascal J. Bourguignon
2015-06-17  0:43         ` Pascal J. Bourguignon
2015-06-17 16:02         ` Phillip Lord
     [not found]         ` <mailman.5167.1434556959.904.help-gnu-emacs@gnu.org>
2015-06-23 23:49           ` Jim Diamond
     [not found] ` <mailman.3950.1432974543.904.help-gnu-emacs@gnu.org>
2015-05-30 12:59   ` Pascal J. Bourguignon
2015-06-14 10:55     ` Marcin Borkowski
     [not found]     ` <mailman.4977.1434279342.904.help-gnu-emacs@gnu.org>
2015-06-14 20:04       ` Stefan Monnier
2015-06-14 21:44         ` Pascal J. Bourguignon
2015-06-14 21:49           ` Pascal J. Bourguignon
     [not found] <mailman.3883.1432888152.904.help-gnu-emacs@gnu.org>
2015-05-29  9:30 ` Joost Kremers
2015-05-29 11:12   ` Andreas Röhler
2015-05-29 12:13     ` Dmitry Gutov
2015-05-29 16:21     ` Phillip Lord
2015-05-29 16:50       ` Yuri Khan
2015-05-29 12:28 ` Pascal J. Bourguignon
2015-05-29 17:16   ` Andreas Röhler
2015-05-29 18:43 ` Emanuel Berg
2015-05-30  5:49 ` Rusi
2015-05-30 12:50   ` Pascal J. Bourguignon
2015-05-30 15:23     ` Rusi
2015-05-30 15:50       ` Pascal J. Bourguignon
2015-05-30 16:21         ` Rusi
2015-05-30 16:03   ` Emanuel Berg
2015-05-30 16:32     ` Rusi
2015-05-30 16:54       ` Pascal J. Bourguignon
2015-05-30 17:10         ` Rusi
2015-05-30 19:12           ` Pascal J. Bourguignon

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=871thatxro.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /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.
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).