emacs-devel
[Top][All Lists]
Advanced

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

Re: pcase defuns


From: Po Lu
Subject: Re: pcase defuns
Date: Thu, 23 Dec 2021 10:30:07 +0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.60 (gnu/linux)

Andrew Hyatt <ahyatt@gmail.com> writes:

> Hi all,
>
> 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.
>
> I find this method of programming quite useful for certain kinds of
> problems, and pcase is close to what I want, but using pcase forced me
> to encapsulate all of my pattern matching logic in one function. Being
> able to have it spread out in pattern matching functions seems much
> nicer to me.
>
> If this is of interest, I can either add this to the pcase module
> itself, create a package in gnu elpa, or (if there's some reason that
> it shouldn't be done) just keep it in a personal repository.
>
> I'll wait for some guidance before creating a final version, with
> tests, wherever it should go.

That would be awfully confusing, even more so than CL generic functions.
It would make locating the right definition of a function an unwarranted
chore.

With a generic function, the methods can usually be found by a simple
search and are usually obvious from context, and the calling convention
is readily available.

That is confusing enough, but with these "pcase defuns", there is no
standard calling convention to advertise.  Now imagine if these pcase
defuns are scattered over different files.

I think the better solution is to do something like this:

  (defun foo (&rest args)
    (pcase args ...

That avoids most of the confusion mentioned above.


reply via email to

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