emacs-devel
[Top][All Lists]
Advanced

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

Re: ELisp futures and continuations/coroutines


From: Ted Zlatanov
Subject: Re: ELisp futures and continuations/coroutines
Date: Thu, 19 May 2011 18:48:49 -0500
User-agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.50 (gnu/linux)

On Thu, 19 May 2011 18:40:47 +0200 Thien-Thi Nguyen <address@hidden> wrote: 

TN> i don't know what such a future type would do.
...
TN> i would guess that a "future type" would be some subset of the fsm
TN> internals.

Why tie futures to fsm.el or deferred.el specifically?  That seems like
an implementation detail.

I'd rather establish a futures API:

- create a future
- is it done?
- get result
- inspect thrown error, if any

which is simple, obviously, but it's essential so all the
implementations that use it speak the same language.

TN> Analogous to the "keymap type", which is really just a list with symbol
TN> ‘keymap’ in its CAR, i am guessing one could poke around fsm internals
TN> and derive a "future type", without having to go to C.

TN> Which is to say, i also don't know enough to say, but i see a similarity.

Yes, it could be that way, or it could be a defstruct as in the
deferred.el Masashi-san posted.  Ideally no C code would be needed.  I
like the defstruct because it's simple yet has structure and accessor
functions.  I would copy the deferred defstruct without the `next' or
`cancel' fields and without any callback/errorback defaults.  Here's a
first cut, pretty trivial code:

#+begin_src lisp
(defstruct future callback errorback status value)

(defun future-done-p (future)
  (future-status future))

(defun future-errored-p (future)
  (eq (future-status future) 'error))

(defun future-finish (future &optional status)
  (unless (future-done-p future)
    (setf (future-status future) (or status t))
    (when (future-callback future)
      (funcall (future-callback future) future))))

(defun future-errored (future error)
  (unless (future-done-p future)
    (setf (future-status future) 'error)
    (when (future-errorback future)
      (funcall (future-errorback future) future error))))

(defun future-call (future)
  (unless (future-done-p future)
    (let ((ff (future-value future)))
      (when (functionp ff)
        ;; TODO: needs error handling
        (setf (future-value future)
              (funcall ff))))
    (future-finish future)))
#+end_src

`future-errored-p' breaks if the return value is 'error, though the
correct callback will be invoked even then.

I like the name "future" because it's fairly standard nowadays, it's a
noun unlike "deferred," there are no global functions or variables with
that string in Emacs except some org-mode variables, and it's very clear.

If the above looks OK I can finish the implementation (error handling in
`future-call' and around the callback/errorback invocations).  Then any
library can use ":include future" to add its own details on top of the
`future' defstruct.

Ted




reply via email to

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