srfi-9.scm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ;;; srfi-9.scm --- define-record-type
  2. ;; Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 2.1 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Commentary:
  18. ;; This module exports the syntactic form `define-record-type', which
  19. ;; is the means for creating record types defined in SRFI-9.
  20. ;;
  21. ;; The syntax of a record type definition is:
  22. ;;
  23. ;; <record type definition>
  24. ;; -> (define-record-type <type name>
  25. ;; (<constructor name> <field tag> ...)
  26. ;; <predicate name>
  27. ;; <field spec> ...)
  28. ;;
  29. ;; <field spec> -> (<field tag> <accessor name>)
  30. ;; -> (<field tag> <accessor name> <modifier name>)
  31. ;;
  32. ;; <field tag> -> <identifier>
  33. ;; <... name> -> <identifier>
  34. ;;
  35. ;; Usage example:
  36. ;;
  37. ;; guile> (use-modules (srfi srfi-9))
  38. ;; guile> (define-record-type :foo (make-foo x) foo?
  39. ;; (x get-x) (y get-y set-y!))
  40. ;; guile> (define f (make-foo 1))
  41. ;; guile> f
  42. ;; #<:foo x: 1 y: #f>
  43. ;; guile> (get-x f)
  44. ;; 1
  45. ;; guile> (set-y! f 2)
  46. ;; 2
  47. ;; guile> (get-y f)
  48. ;; 2
  49. ;; guile> f
  50. ;; #<:foo x: 1 y: 2>
  51. ;; guile> (foo? f)
  52. ;; #t
  53. ;; guile> (foo? 1)
  54. ;; #f
  55. ;;; Code:
  56. (define-module (srfi srfi-9)
  57. :export-syntax (define-record-type))
  58. (cond-expand-provide (current-module) '(srfi-9))
  59. (define-macro (define-record-type type-name constructor/field-tag
  60. predicate-name . field-specs)
  61. `(begin
  62. (define ,type-name
  63. (make-record-type ',type-name ',(map car field-specs)))
  64. (define ,(car constructor/field-tag)
  65. (record-constructor ,type-name ',(cdr constructor/field-tag)))
  66. (define ,predicate-name
  67. (record-predicate ,type-name))
  68. ,@(map
  69. (lambda (spec)
  70. (cond
  71. ((= (length spec) 2)
  72. `(define ,(cadr spec)
  73. (record-accessor ,type-name ',(car spec))))
  74. ((= (length spec) 3)
  75. `(begin
  76. (define ,(cadr spec)
  77. (record-accessor ,type-name ',(car spec)))
  78. (define ,(caddr spec)
  79. (record-modifier ,type-name ',(car spec)))))
  80. (else
  81. (error "invalid field spec " spec))))
  82. field-specs)))
  83. ;;; srfi-9.scm ends here