From 7c1161dba4c8930d3a500a72ccaaaf50df73f466 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Sat, 19 Nov 2022 17:23:04 +0100 Subject: records: 'match-record' checks fields at macro-expansion time. This allows 'match-record' to be more efficient (field offsets are computed at compilation time) and to report unknown fields at macro-expansion time. * guix/records.scm (map-fields): New macro. (define-record-type*)[rtd-identifier]: New procedure. Define TYPE as a macro and use a separate identifier for the RTD. (lookup-field, match-record-inner): New macros. (match-record): Rewrite in terms of 'match-error-inner'. * tests/records.scm ("match-record, simple") ("match-record, unknown field"): New tests. * gnu/services/cuirass.scm (cuirass-shepherd-service): Rename 'log-file' local variable to 'main-log-file'. * gnu/services/getmail.scm (serialize-getmail-configuration-file): Move after definition. --- tests/records.scm | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'tests') diff --git a/tests/records.scm b/tests/records.scm index 00c58b0736..8504c8d5a5 100644 --- a/tests/records.scm +++ b/tests/records.scm @@ -528,4 +528,37 @@ Description: 1st line, '("a" "b" "c") '("a"))) +(test-equal "match-record, simple" + '((1 2) (a b)) + (let () + (define-record-type* foo make-foo + foo? + (first foo-first (default 1)) + (second foo-second)) + + (list (match-record (foo (second 2)) + (first second) + (list first second)) + (match-record (foo (first 'a) (second 'b)) + (second first) + (list first second))))) + +(test-equal "match-record, unknown field" + 'syntax-error + (catch 'syntax-error + (lambda () + (eval '(begin + (use-modules (guix records)) + + (define-record-type* foo make-foo + foo? + (first foo-first (default 1)) + (second foo-second)) + + (match-record (foo (second 2)) + (one two) + #f)) + (make-fresh-user-module))) + (lambda (key . args) key))) + (test-end) -- cgit 1.4.1