diff mbox series

[bug#54652,1/2] guix: Only check the object type once in the MATCH-RECORD expansion.

Message ID 20220331124657.2985-1-attila@lendvai.name
State New
Headers show
Series [bug#54652,1/2] guix: Only check the object type once in the MATCH-RECORD expansion. | expand

Commit Message

Attila Lendvai March 31, 2022, 12:46 p.m. UTC
* guix/records.scm (match-record/fields): New macro.
(match-record): Only check the object type once in the expansion.
---
 guix/records.scm | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

Comments

Attila Lendvai June 30, 2022, 1:56 p.m. UTC | #1
one questionable thing: if fields is the empty list, then it skips entirely the runtime assert that checks the record type.
Attila Lendvai Nov. 20, 2022, 2:18 p.m. UTC | #2
closing, it was superseded by #59390

https://issues.guix.gnu.org/59390
diff mbox series

Patch

diff --git a/guix/records.scm b/guix/records.scm
index ed94c83dac..e311086d73 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -535,17 +535,25 @@  (define (recutils->alist port)
               (else
                (error "unmatched line" line))))))))
 
+(define-syntax match-record/fields
+  (syntax-rules ()
+    ((_ record type (field fields ...) body ...)
+     (let ((field ((record-accessor type 'field) record)))
+       ;; TODO compute indices and report wrong-field-name errors at
+       ;;      expansion time
+       ;; TODO support thunked and delayed fields
+       (match-record/fields record type (fields ...) body ...)))
+    ((_ record type () body ...)
+     (begin body ...))))
+
 (define-syntax match-record
   (syntax-rules ()
     "Bind each FIELD of a RECORD of the given TYPE to it's FIELD name.
 The current implementation does not support thunked and delayed fields."
     ((_ record type (field fields ...) body ...)
      (if (eq? (struct-vtable record) type)
-         ;; TODO compute indices and report wrong-field-name errors at
-         ;;      expansion time
-         ;; TODO support thunked and delayed fields
-         (let ((field ((record-accessor type 'field) record)))
-           (match-record record type (fields ...) body ...))
+         ;; So that we only test the type once.
+         (match-record/fields record type (field fields ...) body ...)
          (throw 'wrong-type-arg record)))
     ((_ record type () body ...)
      (begin body ...))))