From 363b20b68584d71556c2d8912dd07cbdacabdb25 Mon Sep 17 00:00:00 2001 From: Attila Lendvai Date: Wed, 21 Dec 2022 23:14:55 -0300 Subject: records: match-record supports specifying a different variable name. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An example: (match-record obj (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. Signed-off-by: Ludovic Courtès --- guix/records.scm | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'guix/records.scm') 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 @@ found." (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 ...))))) -- cgit v1.2.3