unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Use closures in hashcash.el
@ 2019-03-12  3:12 Christopher Wellons
  2019-03-12 20:37 ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Christopher Wellons @ 2019-03-12  3:12 UTC (permalink / raw)
  To: emacs-devel

hashcash.el was switched over to lexical scope in da94ea9, but it still
uses an old workaround of dynamically constructing lambda expression
"closures" using backquote. The following patch changes these into
proper closures.

I discovered this because, in the first case, the backquote lambda is
actually constructed incorrectly. The callback function object is
evaluated an extra time when the callback is invoked. That's harmless
for non-closures and byte-compiled functions, but it doesn't work with
interpreted closures. I've made this same error myself and have written
about it here:

Emacs Lisp Lambda Expressions Are Not Self-Evaluating
https://nullprogram.com/blog/2018/02/22/

diff --git a/lisp/mail/hashcash.el b/lisp/mail/hashcash.el
index 59200eaab8..77b4a429d2 100644
--- a/lisp/mail/hashcash.el
+++ b/lisp/mail/hashcash.el
@@ -182,8 +182,8 @@ Return immediately.  Call CALLBACK with process and result when ready."
 	(setq hashcash-process-alist (cons
 				      (cons process (current-buffer))
 				      hashcash-process-alist))
-	(set-process-filter process `(lambda (process output)
-				       (funcall ,callback process output))))
+	(set-process-filter process (lambda (process output)
+				      (funcall callback process output))))
     (funcall callback nil nil)))
 
 (defun hashcash-check-payment (token str val)
@@ -244,8 +244,8 @@ Only start calculation.  Results are inserted when ready."
     (hashcash-generate-payment-async
      (hashcash-payment-to arg)
      (hashcash-payment-required arg)
-     `(lambda (process payment)
-	(hashcash-insert-payment-async-2 ,(current-buffer) process payment)))))
+     (lambda (process payment)
+       (hashcash-insert-payment-async-2 (current-buffer) process payment)))))
 
 (defun hashcash-insert-payment-async-2 (buffer process pay)
   (when (buffer-live-p buffer)



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Use closures in hashcash.el
  2019-03-12  3:12 [PATCH] Use closures in hashcash.el Christopher Wellons
@ 2019-03-12 20:37 ` Stefan Monnier
  2019-03-13 15:22   ` Christopher Wellons
  2019-03-13 16:38   ` John Shahid
  0 siblings, 2 replies; 4+ messages in thread
From: Stefan Monnier @ 2019-03-12 20:37 UTC (permalink / raw)
  To: emacs-devel; +Cc: Christopher Wellons

> hashcash.el was switched over to lexical scope in da94ea9, but it still
> uses an old workaround of dynamically constructing lambda expression
> "closures" using backquote. The following patch changes these into
> proper closures.

Thanks, installed with the change below:

> -	(set-process-filter process `(lambda (process output)
> -				       (funcall ,callback process output))))
> +	(set-process-filter process (lambda (process output)
> +				      (funcall callback process output))))

Actually, this lambda expression is just a roundabout way to say
`callback` (by η-reduction).


        Stefan




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Use closures in hashcash.el
  2019-03-12 20:37 ` Stefan Monnier
@ 2019-03-13 15:22   ` Christopher Wellons
  2019-03-13 16:38   ` John Shahid
  1 sibling, 0 replies; 4+ messages in thread
From: Christopher Wellons @ 2019-03-13 15:22 UTC (permalink / raw)
  To: emacs-devel

Michael Heerdegen has pointed out that the second backtick change isn't
correct since it doesn't remember the original buffer. Here's a
correction (sorry!):

diff --git a/lisp/mail/hashcash.el b/lisp/mail/hashcash.el
index 519c6d94e1..6ee1aee55a 100644
--- a/lisp/mail/hashcash.el
+++ b/lisp/mail/hashcash.el
@@ -243,8 +243,9 @@ Only start calculation.  Results are inserted when ready."
     (hashcash-generate-payment-async
      (hashcash-payment-to arg)
      (hashcash-payment-required arg)
-     (lambda (process payment)
-       (hashcash-insert-payment-async-2 (current-buffer) process payment)))))
+     (let ((buffer (current-buffer)))
+       (lambda (process payment)
+         (hashcash-insert-payment-async-2 buffer process payment))))))
 
 (defun hashcash-insert-payment-async-2 (buffer process pay)
   (when (buffer-live-p buffer)



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Use closures in hashcash.el
  2019-03-12 20:37 ` Stefan Monnier
  2019-03-13 15:22   ` Christopher Wellons
@ 2019-03-13 16:38   ` John Shahid
  1 sibling, 0 replies; 4+ messages in thread
From: John Shahid @ 2019-03-13 16:38 UTC (permalink / raw)
  To: emacs-devel; +Cc: Christopher Wellons


Stefan Monnier <monnier@iro.umontreal.ca> writes:

[...]

> Thanks, installed with the change below:
>
>> -	(set-process-filter process `(lambda (process output)
>> -				       (funcall ,callback process output))))
>> +	(set-process-filter process (lambda (process output)
>> +				      (funcall callback process output))))

I think you meant:

-	(set-process-filter process `(lambda (process output)
-				       (funcall ,callback process output))))
+	(set-process-filter process callback))

which is what you indeed installed on master.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-03-13 16:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-03-12  3:12 [PATCH] Use closures in hashcash.el Christopher Wellons
2019-03-12 20:37 ` Stefan Monnier
2019-03-13 15:22   ` Christopher Wellons
2019-03-13 16:38   ` John Shahid

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).