all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: "'Help-Gnu-Emacs (help-gnu-emacs@gnu.org)'" <help-gnu-emacs@gnu.org>
Subject: RE: [External] : Re: function arguments optional default values
Date: Sat, 6 Jul 2024 16:06:17 +0000	[thread overview]
Message-ID: <SJ0PR10MB548859938A092D91E9BC5173F3D82@SJ0PR10MB5488.namprd10.prod.outlook.com> (raw)
In-Reply-To: <jwvjzhya6je.fsf-monnier+emacs@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 2160 bytes --]

> >> > Does elisp support function arguments optional default values ?
> >> I solve that problem with following:
> >> (defun my-function (arg &optional other-arg)
> >>   (let* ((other-arg (or other-arg my-default)))
> >>     other-arg))
> > Simpler - no reason for another binding,
> > just change the value (it's a local var).
> >
> > (defun my-function (arg &optional other-arg)
> >   (setq other-arg  (or other-arg my-default))
> >   ...)
> 
> FWIW, assuming you're using `lexical-binding` (which you should), this
> tends to result in less efficient byte-code.
> 
> The difference is marginal in most cases (and efficiency of ELisp code
> is irrelevant in most cases as well), but the point is that bindings
> don't "cost" as much as mutation.
> 
> Here is how this usually plays out: with (setq x (or x <default>)) the
> evaluation pushes the result of (or x <default>) on the stack, does the
> `setq` which consumes the value on the stack and modifies the value of
> `x`, which itself lives on the stack.
> 
> With (let ((x1 (or x <default>))) ...), the beginning is the same,
> pushing the result of (or x <default>) on the stack, but then the
> execution of the `let` is the "free" act of keeping that value on the
> stack (i.e. doing nothing), and instead of referring to the stack
> element that holds `x`, we will simply refer to the stack element that
> holds `x1`.
> [ There is still a cost to `let` in the fact that the stack grows a bit
>   more, of course, but it's usually of no consequence.  ]
> ... 
>
> The difference is more drastic when the variable is captured by
> a closure because our closure conversion is too naïve to understand that
> the `setq` happens "once and forall" before we capture the variable, so
> it presumes that the var may still be mutated after the closure is
> constructed, which means that the closure can't just hold a copy of the
> var's value but really needs to hold a "reference to the variable",
> which we do by storing the variable's value inside a cons-cell:
> ...

Interesting!  Thanks.

I wonder what the case is with various
Common Lisp implementations.

[-- Attachment #2: winmail.dat --]
[-- Type: application/ms-tnef, Size: 15576 bytes --]

  reply	other threads:[~2024-07-06 16:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-03 21:21 function arguments optional default values Heime
2024-07-03 21:51 ` Stephen Berman
2024-07-03 21:56   ` Heime
2024-07-03 22:04     ` Stephen Berman
2024-07-03 22:24       ` Stephen Berman
2024-07-06 12:43 ` Jean Louis
2024-07-06 15:03   ` [External] : " Drew Adams
2024-07-06 15:55     ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-07-06 16:06       ` Drew Adams [this message]
2024-07-06 19:04         ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-07-06 21:48       ` Heime

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=SJ0PR10MB548859938A092D91E9BC5173F3D82@SJ0PR10MB5488.namprd10.prod.outlook.com \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.