summaryrefslogtreecommitdiff
path: root/sly/records.scm
blob: a7e180d0575c1967f02802f7e2fb7ce19c601f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
;;; Sly
;;; Copyright (C) 2015 David Thompson <davet@gnu.org>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Immutable record type syntax with keyword argument constructor,
;; field inheritance, and default field values.
;;
;;; Code:

(define-module (sly records)
  #:use-module (srfi srfi-9)
  #:export (define-record-type*))

(define-syntax-rule (define-record-type* type
                      constructor keyword-constructor
                      predicate
                      (field getter default) ...)
  "Define a new, immutable record type named TYPE.  PREDICATE is the
name of the procedure that tests if an object is of TYPE.  A GETTER is
the name of the accessor for its respective FIELD.  CONSTRUCTOR is the
name of the record type's low-level constructor that uses positional
arguments corresponding to the order in which the fields are
specified.  KEYWORD-CONSTRUCTOR is the name of the high-level
constructor that uses keyword arguments for each FIELD whose values
correspond to the DEFAULT associated with that field.  Additionally, a
special keyword, #:inherit, can be used to copy values from another
instance of TYPE and only change the fields that are explicitly
overridden."
  (begin
    (define-record-type type
      (constructor field ...)
      predicate
      (field getter) ...)

    (define keyword-constructor
      (let ((default-record (constructor default ...)))
        (lambda* (#:key (inherit default-record)
                        (field (getter inherit)) ...)
          (constructor field ...))))))