jar-defrecord.scm 1.5 KB

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