|
From: | Taylan Kammer |
Subject: | Re: Guix records |
Date: | Wed, 10 Feb 2021 00:14:19 +0100 |
User-agent: | Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 |
Hello, In the module (guix records), there's some very nice syntax rule `define-record-type*` that allows very powerfull declarative style of records. For example: ---------------------------------------------------------------------- (employee (age 30) (name "Foo") (profession "Teacher")) ---------------------------------------------------------------------- I would like to use this feature in my software. However, I don't want to have Guix as a dependency only for that. For now, I've copied the content of (guix records) into (my-software records). But this put burden of maitenance into my hands. Thus, I'm looking for an alternative, perhaps there's a Guile library (other than Guix' module) or a SRFI that offers similar feature?
The most feature-rich record system supported by Guile is probably the R6RS record system, which is available through the modules:
(rnrs records syntactic (6)) (rnrs records procedural (6)) (rnrs records inspection (6))However, it doesn't allow a definition style like in your example, where you explicitly name the fields that you're assigning values to during instance creation.
It does, however, implicitly define getters (and setters for mutable fields).
The Guile documentation is brief. You might want to read the R6RS spec or search for another guide for detailed explanations and examples of how to use the R6RS record system, if it sounds interesting.
Note that it's quite dissimilar to the SRFI-9 system, and some aspects of it are rather complex, which is why many people don't like it. To be honest I find some of those complex features quite ingenious on paper, though I couldn't say how often they would prove to be useful in practice.
Here's a super brief example usage of R6RS records, demonstrating that field accessors are defined implicitly, but constructors still use an unnamed sequence of arguments to assign fields:
(import (rnrs records syntactic (6))) ; must use 'import' for R6RS (define-record-type (cat make-cat cat?) (fields name age color)) (define garfield (make-cat "Garfield" 42 'orange)) (cat-color garfield) ;=> orangeThen there is SRFI-99 which could be seen as an update to SRFI-9, but last I checked it's not in Guile yet. It also doesn't really feature the declarative style like in your example.
- Taylan
[Prev in Thread] | Current Thread | [Next in Thread] |