all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Andrew Hyatt <ahyatt@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: Re: pcase defuns
Date: Sun, 19 Dec 2021 16:08:20 -0500	[thread overview]
Message-ID: <m2tuf48fqz.fsf@andrews-mbp.lan> (raw)
In-Reply-To: <jwvv8zka4zc.fsf-monnier+emacs@gnu.org>

On Sun, Dec 19, 2021 at 12:23 PM Stefan Monnier 
<monnier@iro.umontreal.ca> wrote: 

>> As a part of a personal project, I wrote a way to define 
>> functions in an equivalent way to pcases.  For example: 
>> 
>> (pcase-defun mytest (a b _) 
>>  "Match on 'a 'b with the third argument a wildcard" "a b 
>>  match") 
>> 
>> (pcase-defun mytest (c ,var _) 
>>  "Match on 'c binding VAR, with the third argument a wildcard" 
>>  (format "c %s match" var) ) 
>> 
>> (mytest 'a 'b 'c) -> "a b match" (mytest 'c 100 'c) -> "c 100 
>> match" 
>> 
>> This is all accomplished by a few small but tricky macros and a 
>> hashtable that holds all the rules. 
> 
> This kind of design crossed my mind a few times but I couldn't 
> come up with a way to give it a reasonable semantics and 
> implementation.  Beside the issue of precedence/ordering already 
> mentioned by Tassilo, there's the issue of scoping and 
> order/timing of macroexpansion.  E.g.: 
> 
>     (let ((x 0)) 
>       (pcase-defun mytest (inc-x) 
>         (setq x (1+ x))) 
>       (pcase-defun mytest (get-x) 
>         x)) 
>      (let ((y 0)) 
>       (pcase-defun mytest (inc-y) 
>         (setq y (1+ y))) 
>       (pcase-defun mytest (get-y) 
>         y)) 
> 
> Does this work "right" with your code? 
> 
> Or: 
> 
>     (eval-when-compile (require 'cl-lib)) 
> 
>     (pcase-defun foo (..) 
>       ..  (cl-incf ..)  ..) 
> 
> is `cl-incf` properly macroexpanded during compilation of the 
> file, or is it delayed to when the file is loaded, at which 
> point the `cl-incf` macro may be undefined?

Great points, thank you! Indeed, both of these wouldn't work - I'm 
just quoting and storing the function body for later.  I'll have 
to think about whether there is something more clever I can do 
that would fix these problems.

Does every defun-like thing obey these rules?  I'm not familiar 
with the range of them, so I'm not sure if this would be violating 
convention or not.  If so, one thing I can do is to try to 
distance this from defun perhaps with a different name such as 
`pcase-pattern', and making calls go through a function such as 
`pcase-call'.

>
>
>         Stefan



  reply	other threads:[~2021-12-19 21:08 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-19  4:53 pcase defuns Andrew Hyatt
2021-12-19  8:34 ` Tassilo Horn
2021-12-19 15:33   ` Andrew Hyatt
2021-12-19 17:16     ` Tassilo Horn
2021-12-19 19:05       ` Stefan Monnier
2021-12-20  5:56         ` Tassilo Horn
2021-12-22 14:07   ` LdBeth
2021-12-19 17:23 ` Stefan Monnier
2021-12-19 21:08   ` Andrew Hyatt [this message]
2021-12-21  4:15     ` Richard Stallman
2021-12-21  5:20       ` Andrew Hyatt
2021-12-22  4:18         ` Richard Stallman
2021-12-23  1:52           ` Andrew Hyatt
2021-12-24  4:13             ` Richard Stallman
2021-12-21 15:32     ` Stefan Monnier
2021-12-20  4:43 ` Richard Stallman
2021-12-23  2:30 ` Po Lu
2022-03-26 17:41 ` Andrew Hyatt
2022-03-27  9:31   ` Stefan Monnier
2022-03-27 18:17     ` Andrew Hyatt
2022-03-28  4:15   ` Richard Stallman
2022-03-30  1:28     ` Andrew Hyatt
2022-03-31  4:27       ` Richard Stallman
2022-04-17 22:09         ` Andrew Hyatt
2022-04-19  3:48           ` Richard Stallman

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=m2tuf48fqz.fsf@andrews-mbp.lan \
    --to=ahyatt@gmail.com \
    --cc=emacs-devel@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.