summary refs log tree commit diff
diff options
context:
space:
mode:
authornee <nee.git@cock.li>2017-10-25 20:44:54 +0200
committerLudovic Courtès <ludo@gnu.org>2017-11-05 15:21:53 +0100
commit6692d8454a89e542d85d2b6a93adfd373aeec39c (patch)
tree1ab3e46a2dccf04a2d7c1b9ddc0893061ff7553b
parent50c229caf9b8d09944838f0fb92d3121bbb2a4bc (diff)
downloadguix-6692d8454a89e542d85d2b6a93adfd373aeec39c.tar.gz
guix: records: Add match-record.
* guix/records.scm: New syntax-rule.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
-rw-r--r--guix/records.scm18
1 files changed, 17 insertions, 1 deletions
diff --git a/guix/records.scm b/guix/records.scm
index 7de5fccef6..1f00e16603 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -26,7 +26,8 @@
   #:export (define-record-type*
             alist->record
             object->fields
-            recutils->alist))
+            recutils->alist
+            match-record))
 
 ;;; Commentary:
 ;;;
@@ -375,4 +376,19 @@ pairs.  Stop upon an empty line (after consuming it) or EOF."
               (else
                (error "unmatched line" line))))))))
 
+(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 ...))
+         (throw 'wrong-type-arg record)))
+    ((_ record type () body ...)
+     (begin body ...))))
+
 ;;; records.scm ends here