Suppose you want to have a record type which is only used in the library defines the record type. Later on, you notice that it might be convenient if users can pass the record so you decided to export its constructor.
#!r6rs (library (private-record) (export make-private2) (import (rnrs)) (define-record-type private (fields field1)) (define-record-type private2 (fields field2) (parent private)) ;; you may want to do some ;; private operation here )So far so good. It seems no one can access the record fields except the library itself. So you can have some privacy here.
Is that really so?
Actually not. You would still have peeping Tom. Let's see how we can do it.
#!r6rs (import (rnrs) (private-record)) (define private2 (make-private2 1 2)) (define private2-rtd (record-rtd private2)) ;; predicate (define private? (record-predicate (record-type-parent private2-rtd))) (define private2? (record-predicate private2-rtd)) ;; get field accessors (define (find-accessor rtd field) (let loop ((rtd rtd)) (if rtd (let ((fields (record-type-field-names rtd))) (let lp ((i 0)) (cond ((= i (vector-length fields)) (loop (record-type-parent rtd))) ((eq? (vector-ref fields i) field) (record-accessor rtd i)) (else (lp (+ i 1)))))) (lambda (o) (error 'find-accessor "no such field" field))))) ;; parent field (define private2-field1 (find-accessor private2-rtd 'field1)) ;; record field (define private2-field2 (find-accessor private2-rtd 'field2)) (display (private2? private2)) (newline) (display (private? private2)) (newline) (display (private2-field1 private2)) (newline) (display (private2-field2 private2)) (newline) #| #t #t 1 2 |#Even though the library only exports constructor, we can still access to the fields including parent's one and checks if the object is the particular record.
The inspection layer is really convenient in some cases. Suppose you want to write a destructuring matcher which can also handle records. The essence of this kind of macro would be like the following:
(define-syntax destructuring-record ;; actually we don't need to put 'record' keyword at all ;; to show only this... (syntax-rules (record) ((_ (record r field ...) body ...) (let ((tmp r)) (when (record? tmp) (let* ((rtd (record-rtd tmp)) ;; definition of find-accessor is above (field ((find-accessor rtd 'field) tmp)) ...) body ...)))))) ;; use it (destructuring-record (record private2 field2) (display field2) (newline))If you know the field names of records, then you can write without calling predefined accessors. (Though, this would cost a bit of performance since it creates closures each time.)
How to prevent it?
If you are treating something you really don't want to show such as password (I don't argue holding password here), you just need to specify
(opaque #f)in the record definition.
R7RS or SRFI-9
define-record-typecan be implemented by R6RS's one. Since R7RS's one doesn't specify record inspection, there is no way to prevent this as long as wrapper doesn't specify
(opaque #f). One of the implementations made by Derick Eddington allowed to be inspected. Should this behaviour be the case for R7RS one?