emacs-devel
[Top][All Lists]
Advanced

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

Re: pcase defuns


From: Andrew Hyatt
Subject: Re: pcase defuns
Date: Sun, 19 Dec 2021 16:08:20 -0500

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 via email to

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