[PATCH 1/2] guix: Only check the object type once in the MATCH-RECORD expansion.

  • Done
  • quality assurance status badge
Details
One participant
  • Attila Lendvai
Owner
unassigned
Submitted by
Attila Lendvai
Severity
normal
A
A
Attila Lendvai wrote on 31 Mar 2022 14:46
(address . guix-patches@gnu.org)(name . Attila Lendvai)(address . attila@lendvai.name)
20220331124657.2985-1-attila@lendvai.name
* guix/records.scm (match-record/fields): New macro.
(match-record): Only check the object type once in the expansion.
---
guix/records.scm | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)

Toggle diff (37 lines)
diff --git a/guix/records.scm b/guix/records.scm
index ed94c83dac..e311086d73 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -535,17 +535,25 @@ (define (recutils->alist port)
(else
(error "unmatched line" line))))))))
+(define-syntax match-record/fields
+ (syntax-rules ()
+ ((_ record type (field fields ...) body ...)
+ (let ((field ((record-accessor type 'field) record)))
+ ;; TODO compute indices and report wrong-field-name errors at
+ ;; expansion time
+ ;; TODO support thunked and delayed fields
+ (match-record/fields record type (fields ...) body ...)))
+ ((_ record type () body ...)
+ (begin body ...))))
+
(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 ...))
+ ;; So that we only test the type once.
+ (match-record/fields record type (field fields ...) body ...)
(throw 'wrong-type-arg record)))
((_ record type () body ...)
(begin body ...))))
--
2.34.0
A
A
Attila Lendvai wrote on 31 Mar 2022 14:52
[PATCH 2/2] guix: MATCH-RECORD supports (FIELD-NAME VARIABLE-NAME) forms.
(address . 54652@debbugs.gnu.org)(name . Attila Lendvai)(address . attila@lendvai.name)
20220331125230.4388-2-attila@lendvai.name
E.g. (match-record obj <type> (field1 (field2 var-name)) ...)

* guix/records.scm (match-record/fields): Implement it.
---

a real world example i was facing: a field name is the same
as an object constructor, and thus shadows it in a
match-record form, unless a different variable name is chosen.

guix/records.scm | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

Toggle diff (24 lines)
diff --git a/guix/records.scm b/guix/records.scm
index e311086d73..492ab2aed6 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -537,12 +537,15 @@ (define (recutils->alist port)
(define-syntax match-record/fields
(syntax-rules ()
- ((_ record type (field fields ...) body ...)
- (let ((field ((record-accessor type 'field) record)))
+ ((_ record type ((field-name variable-name) fields ...) body ...)
+ (let ((variable-name ((record-accessor type 'field-name) record)))
;; TODO compute indices and report wrong-field-name errors at
;; expansion time
;; TODO support thunked and delayed fields
(match-record/fields record type (fields ...) body ...)))
+ ((_ record type (field fields ...) body ...)
+ ;; Channel it back into the canonical form above.
+ (match-record/fields record type ((field field) fields ...) body ...))
((_ record type () body ...)
(begin body ...))))
--
2.34.0
A
A
Attila Lendvai wrote on 30 Jun 2022 15:56
(No Subject)
(name . 54652@debbugs.gnu.org)(address . 54652@debbugs.gnu.org)
v_3pX_XtRvCrkLoUHiHs0g5R2kUHZgTxoLEotRkKThwrP-ZMCGK5cKrKAp6h7BYp0g37w70I0XHbSf2hJrw__SUmVno_EaS3fjr4_yJ6_ZQ=@lendvai.name
one questionable thing: if fields is the empty list, then it skips entirely the runtime assert that checks the record type.
A
A
Attila Lendvai wrote on 20 Nov 2022 15:18
(name . 54652-close@debbugs.gnu.org)(address . 54652-close@debbugs.gnu.org)
nSAozmHftO_JWPQo7v7OyaJArInaXPHTIt2koC2da5Amjt13VQd47UkggDl51eLVp6w3q5MD4snArlGb4gOb2M-a2UyGRLObPAUKOHqqWKo=@lendvai.name
closing, it was superseded by #59390


--
• attila lendvai
• PGP: 963F 5D5F 45C7 DFCD 0A39
--
“Personal dishonesty is not needed to produce a dishonest business plan or research proposal. Wishful thinking suffices.”
— John McCarthy (1927–2011), father of Lisp
?