From b7bf25020f146331d161d86ef30df31d2959a8dc Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 21 Sep 2015 19:41:17 -0400 Subject: Add new record type syntax. * sly/record.scm: New file. * Makefile.am (SOURCES): Add it. --- Makefile.am | 1 + sly/record.scm | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 sly/record.scm diff --git a/Makefile.am b/Makefile.am index dad4409..f7aa9cf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -42,6 +42,7 @@ SOURCES = \ sly/repl.scm \ sly/signal.scm \ sly/window.scm \ + sly/record.scm \ sly/render/utils.scm \ sly/render/color.scm \ sly/render/camera.scm \ diff --git a/sly/record.scm b/sly/record.scm new file mode 100644 index 0000000..8221b14 --- /dev/null +++ b/sly/record.scm @@ -0,0 +1,54 @@ +;;; Sly +;;; Copyright (C) 2015 David Thompson +;;; +;;; 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 +;;; . + +;;; Commentary: +;; +;; Immutable record type syntax with keyword argument constructor, +;; field inheritance, and default field values. +;; +;;; Code: + +(define-module (sly record) + #: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 ...)))))) -- cgit v1.2.3