help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Emacs Lisp style on callbacks?


From: Pascal J. Bourguignon
Subject: Re: Emacs Lisp style on callbacks?
Date: Wed, 08 Dec 2010 15:32:50 -0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

Elena <egarrulo@gmail.com> writes:
> this a question about style.
>
> I'm writing an asynchronous function which upon completion will
> execute a callback.  Would you suggest using separate callbacks for
> successful and unsuccessful completion? or would you suggest using the
> same callback which takes a cons with a success flag as its car?  Any
> other suggestions?

It could as well take several arguments.

If the callback when an error occurs needs different arguments than the
callback when it's ok, 

or if it is expected that only one of the two callbacks will be provided
normally (relying on a default behavior for the other),

then you may want to have two different callbacks.


> I know Emacs Lisp lacks closures: the function would take care of all
> additional book-keeping.

On the other hand, given the lack of closures, it may be easier to have
to provide only one callback, where the book-keeping will be taken care
for both.




Finally, you could also (require 'cl) and use lexical-let to have the
lambdas create lexical closures.

(lexical-let ((x x)
              (y y))
   (do-something object
         :when-done (lambda (object elapsed-time)
                      (message "success with %S for %S in %S s"
                                object x elapsed-time))
         :on-error  (lambda (object error-code error-message)
                      (message "got an error with %S for %S: %s"
                               object y error-message))))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


reply via email to

[Prev in Thread] Current Thread [Next in Thread]