mbox series

[bug#59390,0/5] Doing 'match-record' work at expansion time

Message ID 20221119222326.10644-1-ludo@gnu.org
Headers show
Series Doing 'match-record' work at expansion time | expand

Message

Ludovic Courtès Nov. 19, 2022, 10:23 p.m. UTC
Hello Guix!

This addresses a longstanding issue: making ‘match-record’ efficient,
and allowing it to error out on unknown field names are macro-expansion
time.

It does so by changing the record type identifier passed to
‘define-record-type*’ to a macro that can either expand to the actual
record type descriptor (RTD) or provide the list of fields of
this type:

--8<---------------cut here---------------start------------->8---
scheme@(guix records)> (define-record-type* <foo> foo make-foo foo? (one foo-one) (two foo-two))
scheme@(guix records)> <foo>
$89 = #<record-type <foo>>
scheme@(guix records)> ,expand <foo>
$90 = #{% <foo> rtd}#
scheme@(guix records)> ,expand (<foo> map-fields f)
$91 = (f (one two))
scheme@(guix records)> ,optimize (match-record x <foo>
				   (two one)
				   (list one two))
$92 = (if (eq? (struct-vtable x) #{% <foo> rtd}#)
  (let* ((two (struct-ref x 1)) (one (struct-ref x 0)))
    (list one two))
  (throw 'wrong-type-arg x))
scheme@(guix records)> ,expand (match-record x <foo>
				 (xyz)
				 #f)
While executing meta-command:
Syntax error:
unknown file:12066:34: lookup-field: unknown record type field in subform xyz of (lookup-field xyz (+ 1 (+ 1 0)) ())
--8<---------------cut here---------------end--------------->8---

I changed a few services that were using ‘match’ to use either
‘match-record’ or accessors (the latter when accessing just one
or two fields).

This change breaks the ABI: we’ll have to run:

  make clean-go && make

Thoughts?

Ludo’.

Ludovic Courtès (5):
  records: 'match-record' checks fields at macro-expansion time.
  doc: Recommend 'match-record'.
  home: services: Use 'match-record' instead of 'match'.
  services: base: Use 'match-record' instead of 'match'.
  services: networking: Avoid 'match' on records.

 doc/contributing.texi        |   7 +-
 gnu/home/services/mcron.scm  |  58 +--
 gnu/home/services/shells.scm |  50 +-
 gnu/home/services/xdg.scm    |  36 +-
 gnu/services/base.scm        | 882 +++++++++++++++++------------------
 gnu/services/cuirass.scm     |   4 +-
 gnu/services/getmail.scm     |  22 +-
 gnu/services/networking.scm  | 661 +++++++++++++-------------
 guix/records.scm             |  87 +++-
 tests/records.scm            |  33 ++
 10 files changed, 967 insertions(+), 873 deletions(-)


base-commit: bb04b5e0ceb606c8d33d53bf06f7fc8855a2c56b

Comments

Ludovic Courtès Dec. 1, 2022, 11:07 p.m. UTC | #1
Ludovic Courtès <ludo@gnu.org> skribis:

> This addresses a longstanding issue: making ‘match-record’ efficient,
> and allowing it to error out on unknown field names at macro-expansion
> time.

I went ahead, rebased, and pushed these:

  00ddf185e6 services: networking: Avoid 'match' on records.
  adfe1064c8 services: base: Use 'match-record' instead of 'match'.
  4c8eea027a home: services: Use 'match-record' instead of 'match'.
  cc9ee514e3 doc: Recommend 'match-record'.
  7c1161dba4 records: 'match-record' checks fields at macro-expansion time.

This change breaks the ABI: we’ll have to run:

  make clean-go && make

Inquiries welcome!

Ludo’.