summaryrefslogtreecommitdiff
path: root/sly/records.scm
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2016-01-11 08:41:28 -0500
committerDavid Thompson <dthompson2@worcester.edu>2016-01-12 08:38:50 -0500
commit15c54d58064d825cf71507baa05269d03a21caa3 (patch)
tree3e218c9107d5a95ca722d5632a8636b8dc3d1713 /sly/records.scm
parentd21b67eaad50c9389e1ad83cc595d2e77783942c (diff)
Rename (sly record) to (sly records).
* sly/record.scm: Delete. * sly/records.scm: New file. * Makefile.am (SOURCES): Remove deleted file. Add new file.
Diffstat (limited to 'sly/records.scm')
-rw-r--r--sly/records.scm54
1 files changed, 54 insertions, 0 deletions
diff --git a/sly/records.scm b/sly/records.scm
new file mode 100644
index 0000000..a7e180d
--- /dev/null
+++ b/sly/records.scm
@@ -0,0 +1,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 ...))))))