bug-guix
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#51383: noobie way of incorrectly using (guix records)


From: zimoun
Subject: bug#51383: noobie way of incorrectly using (guix records)
Date: Mon, 25 Oct 2021 09:48:53 +0200

Hi,

On Mon, 25 Oct 2021 at 02:15, Joshua Branson via Bug reports for GNU Guix 
<bug-guix@gnu.org> wrote:

> So I made a pretty noobie-like mistake a few minutes ago.  When one
> tries to make a (record-configuration), he invariably create an
> infinite number of records.  The guile compiler eventually runs out
> of memory and stops compiling.
>
> (use-modules (guix records))
>
> (define-record-type* <record-configuration>
>   record-configuration make-record-configuration
>   record-configuration?
>   (command record-configuration-command
>            ;; the error is here is on the next line
>            (default (record-configuration))))  
>
> (record-configuration)

This <record-configuration> is defined by creating recursively another
instance.  Thus, It is expected that it does not work, no?

Reading the doc,

  1. what do you want to achieve?
  2. what does it appear to you buggy?  Or what do you think the
     “correct” behaviour should be?

--8<---------------cut here---------------start------------->8---
(define-syntax define-record-type*
  (lambda (s)
    "Define the given record type such that an additional \"syntactic
constructor\" is defined, which allows instances to be constructed with named
field initializers, à la SRFI-35, as well as default values.  An example use
may look like this:

  (define-record-type* <thing> thing make-thing
    thing?
    this-thing
    (name  thing-name (default \"chbouib\"))
    (port  thing-port
           (default (current-output-port)) (thunked))
    (loc   thing-location (innate) (default (current-source-location))))

This example defines a macro 'thing' that can be used to instantiate records
of this type:

  (thing
    (name \"foo\")
    (port (current-error-port)))

The value of 'name' or 'port' could as well be omitted, in which case the
default value specified in the 'define-record-type*' form is used:

  (thing)

The 'port' field is \"thunked\", meaning that calls like '(thing-port x)' will
actually compute the field's value in the current dynamic extent, which is
useful when referring to fluids in a field's value.  Furthermore, that thunk
can access the record it belongs to via the 'this-thing' identifier.

A field can also be marked as \"delayed\" instead of \"thunked\", in which
case its value is effectively wrapped in a (delay …) form.

A field can also have an associated \"sanitizer\", which is a procedure that
takes a user-supplied field value and returns a \"sanitized\" value for the
field:

  (define-record-type* <thing> thing make-thing
    thing?
    this-thing
    (name  thing-name
           (sanitize (lambda (value)
                       (cond ((string? value) value)
                             ((symbol? value) (symbol->string value))
                             (else (throw 'bad! value)))))))

It is possible to copy an object 'x' created with 'thing' like this:

  (thing (inherit x) (name \"bar\"))

This expression returns a new object equal to 'x' except for its 'name'
field and its 'loc' field---the latter is marked as \"innate\", so it is not
inherited."
--8<---------------cut here---------------end--------------->8---

(Argh, I do not know how to read/display the docstring from the REPL,
another annoying* story. :-))


> This is not possible with (srfi sfri-9)
>
> (use-modules (srfi srfi-9))
>
> (define-record-type <employee>
>   (make-employee name age (make-employeee 5 5 5))
>   employee?
>   (name    employee-name)
>   (age     employee-age    set-employee-age!)
>   (salary  employee-salary set-employee-salary!))

Well, ’(guix records)’ allows to do more than ’(srfi srfi-9)’.  Aside, I
am not convinced that this latter snippet is similar than the former
previous one.


Cheers,
simon


*annoying REPL, I get:

--8<---------------cut here---------------start------------->8---
scheme@(guix-user)> ,describe define-record-type*
While executing meta-command:
Syntax error:
unknown file:79:10: source expression failed to match any pattern in form 
define-record-type*
--8<---------------cut here---------------end--------------->8---





reply via email to

[Prev in Thread] Current Thread [Next in Thread]