summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.dir-locals.el1
-rw-r--r--guix/read-print.scm1
-rw-r--r--guix/records.scm14
-rw-r--r--tests/records.scm12
4 files changed, 27 insertions, 1 deletions
diff --git a/.dir-locals.el b/.dir-locals.el
index 3ffd25ee94e..d79b5c9d7e3 100644
--- a/.dir-locals.el
+++ b/.dir-locals.el
@@ -71,6 +71,7 @@
71 (eval . (put 'lambda* 'scheme-indent-function 1)) 71 (eval . (put 'lambda* 'scheme-indent-function 1))
72 (eval . (put 'substitute* 'scheme-indent-function 1)) 72 (eval . (put 'substitute* 'scheme-indent-function 1))
73 (eval . (put 'match-record 'scheme-indent-function 3)) 73 (eval . (put 'match-record 'scheme-indent-function 3))
74 (eval . (put 'match-record-lambda 'scheme-indent-function 2))
74 75
75 ;; TODO: Contribute these to Emacs' scheme-mode. 76 ;; TODO: Contribute these to Emacs' scheme-mode.
76 (eval . (put 'let-keywords 'scheme-indent-function 3)) 77 (eval . (put 'let-keywords 'scheme-indent-function 3))
diff --git a/guix/read-print.scm b/guix/read-print.scm
index 04dc0dcfbe7..25be289d60a 100644
--- a/guix/read-print.scm
+++ b/guix/read-print.scm
@@ -319,6 +319,7 @@ expressions and blanks that were read."
319 ('letrec* 2) 319 ('letrec* 2)
320 ('match 2) 320 ('match 2)
321 ('match-record 3) 321 ('match-record 3)
322 ('match-record-lambda 2)
322 ('when 2) 323 ('when 2)
323 ('unless 2) 324 ('unless 2)
324 ('package 1) 325 ('package 1)
diff --git a/guix/records.scm b/guix/records.scm
index cfa46f0d808..2a88cb4b3cb 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -31,7 +31,8 @@
31 alist->record 31 alist->record
32 object->fields 32 object->fields
33 recutils->alist 33 recutils->alist
34 match-record)) 34 match-record
35 match-record-lambda))
35 36
36;;; Commentary: 37;;; Commentary:
37;;; 38;;;
@@ -640,4 +641,15 @@ an unknown field is queried."
640 (match-record-inner record type (fields ...) body ...) 641 (match-record-inner record type (fields ...) body ...)
641 (throw 'wrong-type-arg record))))) 642 (throw 'wrong-type-arg record)))))
642 643
644(define-syntax match-record-lambda
645 (syntax-rules ()
646 "Return a procedure accepting a single record of the given TYPE for which each
647FIELD will be bound to its FIELD name within the returned procedure. A syntax error
648is raised if an unknown field is queried."
649 ((_ type (field ...) body ...)
650 (lambda (record)
651 (if (eq? (struct-vtable record) type)
652 (match-record-inner record type (field ...) body ...)
653 (throw 'wrong-type-arg record))))))
654
643;;; records.scm ends here 655;;; records.scm ends here
diff --git a/tests/records.scm b/tests/records.scm
index 4f0aeb39035..8ee306bddc5 100644
--- a/tests/records.scm
+++ b/tests/records.scm
@@ -590,4 +590,16 @@ Description: 1st line,
590 (match-record rec <with-thunked> (normal thunked) 590 (match-record rec <with-thunked> (normal thunked)
591 (list normal thunked))))) 591 (list normal thunked)))))
592 592
593(test-equal "match-record-lambda"
594 '("thing: foo" "thing: bar")
595 (begin
596 (define-record-type* <with-text> with-text make-with-text
597 with-text?
598 (text with-text-text))
599
600 (map (match-record-lambda <with-text> (text)
601 (string-append "thing: " text))
602 (list (with-text (text "foo"))
603 (with-text (text "bar"))))))
604
593(test-end) 605(test-end)