jar-defrecord.scm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees, Robert Ransom
  3. ; This is JAR's define-record-type, which doesn't resemble Richard's.
  4. ; There's no implicit name concatenation, so it can be defined
  5. ; entirely using syntax-rules. Example:
  6. ; (define-record-type foo :foo
  7. ; (make-foo x y)
  8. ; foo? - predicate name is optional
  9. ; (x foo-x)
  10. ; (y foo-y)
  11. ; (z foo-z set-foo-z!))
  12. (define-syntax define-record-type
  13. (syntax-rules ()
  14. ((define-record-type ?type ; compatibility with SRFI 9
  15. (?constructor ?arg ...) . ?more)
  16. (define-record-type ?type ?type
  17. (?constructor ?arg ...) . ?more))
  18. ((define-record-type ?id ?type
  19. (?constructor ?arg ...)
  20. (?field . ?field-stuff)
  21. ...)
  22. (begin (define ?type (make-record-type '?id '(?field ...)))
  23. (define ?constructor (record-constructor ?type '(?arg ...)))
  24. (define-accessors ?type (?field . ?field-stuff) ...)))
  25. ((define-record-type ?id ?type
  26. (?constructor ?arg ...)
  27. ?pred
  28. ?more ...)
  29. (begin (define-record-type ?id ?type
  30. (?constructor ?arg ...)
  31. ?more ...)
  32. (define ?pred (record-predicate ?type))))))
  33. ; Straightforward version
  34. (define-syntax define-accessors
  35. (syntax-rules ()
  36. ((define-accessors ?type ?field-spec ...)
  37. (begin (define-accessor ?type . ?field-spec) ...))))
  38. (define-syntax define-accessor
  39. (syntax-rules ()
  40. ((define-accessor ?type ?field ?accessor)
  41. (define ?accessor (record-accessor ?type '?field)))
  42. ((define-accessor ?type ?field ?accessor ?modifier)
  43. (begin (define ?accessor (record-accessor ?type '?field))
  44. (define ?modifier (record-modifier ?type '?field))))
  45. ((define-accessor ?type ?field)
  46. (begin))))