records: match-record supports specifying a different variable name.

An example:

(match-record obj <my-type>
  (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 <ludo@gnu.org>
This commit is contained in:
Attila Lendvai 2022-12-21 23:14:55 -03:00 committed by Ludovic Courtès
parent 361aad5ce3
commit 363b20b685
No known key found for this signature in database
GPG Key ID: 090B11993D9AEBB5
2 changed files with 8 additions and 5 deletions

View File

@ -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 ...)))))

View File

@ -540,8 +540,8 @@ Description: 1st line,
(first second)
(list first second))
(match-record (foo (first 'a) (second 'b)) <foo>
(second first)
(list first second)))))
(second (first first/new-var))
(list first/new-var second)))))
(test-equal "match-record, unknown field"
'syntax-error