Anonymous records Antero Mejr (24 Sep 2024 03:15 UTC)
Re: Anonymous records Jakub T. Jankiewicz (24 Sep 2024 09:20 UTC)
Re: Anonymous records Daphne Preston-Kendal (24 Sep 2024 16:17 UTC)
Re: Anonymous records Marc Nieper-Wißkirchen (24 Sep 2024 17:18 UTC)

Anonymous records Antero Mejr 24 Sep 2024 03:14 UTC

Lately I've been thinking SML-style anonymous records would be a more
composable improvement over define-record-type (in its various
incarnations).

It could also be a way to do keyword arguments. Something like:

```
;; Defines a point record with x = y = 10.
(define point (record 'x 10 'y 10))

(define x-value (record-ref point 'x)) ; 10

;; A newly-allocated point with x = 10, y = 20:
(define new-point (record-update point 'y 20))

;; Or mutate:
(record-set! point 'y 20)

;; Anonymous record types are equivalent if they have the same fields.
;; record-predicate helper returns a 1-argument predicate procedure:
(define point? (record-predicate point))
(point? (record x '1 'y 2)) ; #t

;; Take a record argument to do "keyword" arguments. A macro can be
;; provided for defining procedures with kw arguments more cleanly.
(define (add-numbers a b . keywords)
  (let ((keywords (if (null? keywords) (record 'c 100) (car keywords))))
    (+ a b (record-ref keywords 'c))))

;; kw: is an alias for record
(add-numbers 1 2 (kw: 'c 3))

;; Inheritance can be done like this:
(define point-3d (extend-record point 'z 50))
```

It is possible to implement define-record-type in terms of anonymous
records. Features like record constructors with default values and
type-checking are also relatively easy to implement and portable.

Would there be any interest in an anonymous records SRFI?