[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: opaque type proposal




I think the opaque type proposal is excellent, and is sorely needed in order for
Scheme to become a language with real abstract data types.

However, I object to the fact that the constructor for a record initializes all
the fields of the record.  This is:

1) An unnecessary bundling of functionality.

2) An annoyance for large structures.

3) Inefficient if bogus values must be specified in the case where the
programmer does not care, especially for large structures.

4) Eliminates possible error checking of uninitialized fields.


An alternative is to have the RECORD-CONSTRUCTOR function take a list of fields
that the constructor it returns should initialize.  For example:

(define yow-record-type (make-record-type 'yow '(a b c)))

;;; This one initializes all three fields, as before.
(define yow-constructor (record-constructor yow-record-type '(a b c)))

;;; This one only initializes A and B. B is the first argument to the function.
;;; A is the second.
(define yow-constructor-no-c (record-constructor yow-record-type '(b a)))





Also, one comment on Pavel's implementation of single inheritence.  The check of
the length of the record can be eliminated by consing a new object to represent
the type of the record, and maintaining the invariant that this newly consed
object only ever appears in a record of that type.  Taking Pavel's example:

   
   	((record-constructor three) 1 2 3 4 5 6)
   
   would be a length 9 record like this:
   
   	Slot	Slot
   	Index	Value
   	-----	-----
   	  0	one-tag
   	  1	1		; value for the A slot
   	  2	2		; value for the B slot
   	  3	two-tag
   	  4	3		; value for the C slot
   	  5	4		; value for the D slot
   	  6	three-tag
   	  7	5		; value for the E slot
   	  8	6		; value for the F slot
   

TWO-TAG would be a newly consed object that is only ever present in the heap as
the third slot of a record of type TWO-TAG (note this implies that TWO-TAG could
not be the "record-type" since that is a first class object).  Now to check
whether a record is of type TWO-TAG, one need only check to see if its third
slot is EQ to TWO-TAG.  Implementing this is somewhat tricky, but definitely
possible.