From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Tomas Hlavaty Newsgroups: gmane.emacs.devel Subject: Re: continuation passing in Emacs vs. JUST-THIS-ONE Date: Mon, 27 Mar 2023 01:50:51 +0200 Message-ID: <87wn332ht0.fsf@logand.com> References: <627090382.312345.1678539189382@office.mailbox.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32348"; mail-complaints-to="usenet@ciao.gmane.io" To: Thomas Koch , "emacs-devel@gnu.org" Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Mar 27 01:51:55 2023 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pga9T-000865-2I for ged-emacs-devel@m.gmane-mx.org; Mon, 27 Mar 2023 01:51:55 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pga8Y-0001GS-Ms; Sun, 26 Mar 2023 19:50:58 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pga8X-0001Fx-0F for emacs-devel@gnu.org; Sun, 26 Mar 2023 19:50:57 -0400 Original-Received: from logand.com ([37.48.87.44]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pga8V-0004lw-GK for emacs-devel@gnu.org; Sun, 26 Mar 2023 19:50:56 -0400 Original-Received: by logand.com (Postfix, from userid 1001) id 836AB19E65B; Mon, 27 Mar 2023 01:50:53 +0200 (CEST) X-Mailer: emacs 28.2 (via feedmail 11-beta-1 I) In-Reply-To: <627090382.312345.1678539189382@office.mailbox.org> Received-SPF: pass client-ip=37.48.87.44; envelope-from=tom@logand.com; helo=logand.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:304780 Archived-At: On Sat 11 Mar 2023 at 14:53, Thomas Koch wrote: > While I don't know elisp, I unfortunately had to do JavaScript. Like > Emacs, JS is single-threaded. While I share the sentiment about JS, > there are still things to learn from it, e.g. event driven > programming. I guess you mean async & await as opposed to callback hell. I think that the essence of async & await is to teleport a value from one place to another. It has nothing to do with asynchronicity. It just happens that this is useful with asynchronous code where it is convenient to teleport a value from under one stack (or thread) of execution to under the current stack (or thread) of execution. (await <- 2) to the current place (async ... (yield 42))) <- 1) teleport 42 => 42 async is just a syntactic sugar to lexically provide the necessary facilities to make this teleportation work. Thanks to lexical binding and lisp macros, this is easy work for the lisp compiler: (defun await (future) (let (z) (while (eq 'EAGAIN (setq z (funcall future))) (sit-for 0.2)) z)) (defmacro async (&rest body) (declare (indent 0)) (let ((z (gensym)) (e (gensym))) `(let (,e (,z 'EAGAIN)) (cl-flet ((yield (x) (setq ,z x)) (fail (string &rest args) (setq ,e (cons string args)))) ,@body) (lambda () (if ,e (apply #'error ,e) ,z))))) Doing it this way brings great flexibility in what the three dots in the sketch above can be: synchronous code in the current thread of execution, asynchronous process with its filter or sentinel callback, another thread or maybe a timer loop like in javascript.