diff mbox series

[bug#60225,v2,1/2] records: match-record supports specifying a different variable name.

Message ID 20221222021455.18632-1-attila@lendvai.name
State New
Headers show
Series [bug#60225,v2,1/2] records: match-record supports specifying a different variable name. | expand

Commit Message

Attila Lendvai Dec. 22, 2022, 2:14 a.m. UTC
An example:

(match-record obj <my-type>
  (field1 (field2 custom-var-name) field3)
  ...)

* guix/records.scm (match-record-inner): Add support for the new syntax.
* tests/records.scm ("match-record, simple"): Add a simple test case for the
new syntax.
---
 guix/records.scm  | 9 ++++++---
 tests/records.scm | 4 ++--
 2 files changed, 8 insertions(+), 5 deletions(-)

Comments

Ludovic Courtès Dec. 27, 2022, 10:49 p.m. UTC | #1
Hi,

Attila Lendvai <attila@lendvai.name> skribis:

> An example:
>
> (match-record obj <my-type>
>   (field1 (field2 custom-var-name) field3)
>   ...)
>
> * guix/records.scm (match-record-inner): Add support for the new syntax.
> * tests/records.scm ("match-record, simple"): Add a simple test case for the
> new syntax.

Applied, thanks!

Ludo’.
diff mbox series

Patch

diff --git a/guix/records.scm b/guix/records.scm
index 13463647c8..1f097c7108 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -592,13 +592,16 @@  (define-syntax lookup-field
 (define-syntax match-record-inner
   (lambda (s)
     (syntax-case s ()
-      ((_ record type (field rest ...) body ...)
-       #`(let-syntax ((field-offset (syntax-rules ()
+      ((_ record type ((field variable) rest ...) body ...)
+       #'(let-syntax ((field-offset (syntax-rules ()
 			              ((_ f)
                                        (lookup-field field 0 f)))))
            (let* ((offset (type map-fields field-offset))
-                  (field  (struct-ref record offset)))
+                  (variable (struct-ref record offset)))
              (match-record-inner record type (rest ...) body ...))))
+      ((_ record type (field rest ...) body ...)
+       ;; Redirect to the canonical form above.
+       #'(match-record-inner record type ((field field) rest ...) body ...))
       ((_ record type () body ...)
        #'(begin body ...)))))
 
diff --git a/tests/records.scm b/tests/records.scm
index 8504c8d5a5..b1203dfeb7 100644
--- a/tests/records.scm
+++ b/tests/records.scm
@@ -540,8 +540,8 @@  (define-record-type* <foo> foo make-foo
             (first second)
             (list first second))
           (match-record (foo (first 'a) (second 'b)) <foo>
-            (second first)
-            (list first second)))))
+            (second (first first/new-var))
+            (list first/new-var second)))))
 
 (test-equal "match-record, unknown field"
   'syntax-error