unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Docs for &optional and &rest arguments together
@ 2020-12-29 13:26 Arthur Miller
  2020-12-29 15:10 ` Adam Porter
  2020-12-30  3:12 ` Lars Ingebrigtsen
  0 siblings, 2 replies; 23+ messages in thread
From: Arthur Miller @ 2020-12-29 13:26 UTC (permalink / raw)
  To: emacs-devel


I like 'with-*' idiom in Lisp, and I have written a smal macro for
myself to work with files, based on with-temp-file macro.

The Manual says I can have both optional and rest arguments togheter,
which we can: 

(required-vars…
 [&optional [optional-vars…]]
 [&rest [rest-var]])

But when I use it, I still have to pass a nil for the "optional"
argument, which I think is also not so strange either, otherwise how
will Emacs now where "optional" argument list ends and where "rest"
argument list starts? No?

When I read the manual:

"A call to the function requires one actual argument for each of the
required-vars. There may be actual arguments for zero or more of the
optional-vars, and there cannot be any actual arguments beyond that
unless the lambda list uses &rest. In that case, there may be any number
of extra actual arguments.

If actual arguments for the optional and rest variables are omitted,
then they always default to nil."

https://www.gnu.org/software/emacs/manual/html_node/elisp/Argument-List.html

I get the impression that I actually can omit the optional argument(s)
even when followed by the &rest keyword. It is probably only the case
when optional arguments *are not* followed by the &rest keyword. I don't
see that captured by the documentation, at least not very clearly.

After the experience with add-to-list I am not offering any patches for
the docs. I am just pointing it out how I perceive it. If I am wrong
about, I would be actually happy to see how to use both &optional and
&rest and not have to specify the "optional" argument when I call the macro.

For the illustration of what I describe above, here is the macro:

(defmacro with-file (file &optional operation &rest body)
  (declare (indent 1) (debug t))
  `(let ((op ,operation)
         (buffer (get-buffer-create ,file)))
     (unless op
       (setq op 'append))
     (unwind-protect
         (prog1 
             (with-current-buffer buffer
                   (cond ((equal op 'apend)
                          (goto-char (point-min))
                          (insert-file-contents ,file)
                          (goto-char (point-max))
                          ,@body)
                         ((equal op 'prepend)
                          ,@body
                          (goto-char (point-max))
                          (insert-file-contents ,file))
                         (t ;; overwrite file
                          ,@body)))
	   (with-current-buffer buffer
	     (write-region nil nil ,file nil 0)))
       (and (buffer-name buffer)
            (kill-buffer buffer)))))

And when calling, there is no way to omit the "optional" nil:

(with-file "some-test" nil
           (insert "hello world"))

(with-file "some-test" 'prepend
           (insert "I am before Hello world!")
           (newline))

(with-file "some-test" 'apend
           (newline)
           (insert "I am after Hello world!"))



^ permalink raw reply	[flat|nested] 23+ messages in thread
* RE: Docs for &optional and &rest arguments together
@ 2020-12-31 20:04 arthur miller
  2020-12-31 20:35 ` tomas
  0 siblings, 1 reply; 23+ messages in thread
From: arthur miller @ 2020-12-31 20:04 UTC (permalink / raw)
  To: tomas@tuxteam.de; +Cc: emacs-devel@gnu.org

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

"I know, it's difficult. It's as difficult for me to see your point.
I'm not being deliberately dense (I take you don't assume that
either). At least not more than I am by nature :)"

Of course not! I am thankful for all input.

Thanks! Hope new one will get better. Corona sucked :-)

-------- Originalmeddelande --------
Från: tomas@tuxteam.de
Datum: 2020-12-31 20:41 (GMT+01:00)
Till: arthur miller <arthur.miller@live.com>
Kopia: emacs-devel@gnu.org
Ämne: Re: Docs for &optional and &rest arguments together

On Thu, Dec 31, 2020 at 05:08:10PM +0000, arthur miller wrote:
> It Is flera för me hos it work. What I am saying is that docs does not say that we can't omit "optional" c or d when &rest is used. In the examples you are illustrating with, one can't omit say d and write (foo a b  c  e), both c and d are required, so user has to pass explicitly at least nil if not d is provided: (foo a b c nil e)
>
> Documentation is talking about being able to omit either optional or rest arga, but does not touch on the case when one pass both optional and rest together. In that case optional is not optional any longer.
>
> I don't understand why is it so difficult to see what I am talking about :-).

I know, it's difficult. It's as difficult for me to see your point.
I'm not being deliberately dense (I take you don't assume that
either). At least not more than I am by nature :)

> Anyway: Happy new year's eve and all best in 2021!

Likewise. All the best!

Cheers
- t

[-- Attachment #2: Type: text/html, Size: 2081 bytes --]

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

end of thread, other threads:[~2021-01-01 14:33 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-29 13:26 Docs for &optional and &rest arguments together Arthur Miller
2020-12-29 15:10 ` Adam Porter
2020-12-29 17:06   ` arthur miller
2021-01-01 14:33   ` Arthur Miller
2020-12-30  3:12 ` Lars Ingebrigtsen
2020-12-30 12:19   ` Arthur Miller
2020-12-30 12:54     ` Yuri Khan
2020-12-31  4:43     ` Lars Ingebrigtsen
2020-12-31  7:55       ` arthur miller
2020-12-31 11:26         ` tomas
2020-12-31 16:45           ` Drew Adams
2020-12-31 17:04             ` Drew Adams
2020-12-31 17:28             ` arthur miller
2020-12-31 18:19               ` Drew Adams
2020-12-31 20:01                 ` arthur miller
2020-12-31 20:27                   ` Drew Adams
2020-12-31 17:08           ` arthur miller
2020-12-31 17:30             ` Daniel Brooks
2020-12-31 19:53               ` arthur miller
2020-12-31 19:40             ` tomas
  -- strict thread matches above, loose matches on Subject: below --
2020-12-31 20:04 arthur miller
2020-12-31 20:35 ` tomas
2020-12-31 23:18   ` arthur miller

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