[gambit-list] define-type with constraints

Marc Feeley feeley at iro.umontreal.ca
Thu May 21 23:26:00 EDT 2009

On 21-May-09, at 3:22 PM, lowly coder wrote:

> I realize the purpose of define-type is to create structs, but I  
> have struct with some constarints that must be satisfied, like in  
> define-type.scm
> I want two vars, 'x' and 'twice-x', where twice-x = (* 2 x)
> In my code, I have created good-x-set! and good-twice-x-set!
> However, how do I overwrite the existing linked-x-set! and linked- 
> twice-x-set! with my good-x-set! and good-twice-x-set! ?

Give private names for the actual setters, and define linked-x-set!  
and linked-twice-x-set! so
that they call the private setter procedures.


(define-type linked
   (x linked-x $linked-x-set!)
   (twice-x linked-twice-x $linked-twice-x-set!))

(define (linked-x-set! v x)
   ($linked-x-set! v x)
   ($linked-twice-x-set! v (* x 2)))

(define (linked-twice-x-set! v x)
   ($linked-x-set! v (/ x 2))
   ($linked-twice-x-set! v x))

(define obj (make-linked 0 0))

(linked-x-set! obj 11)

(pp obj)

(linked-twice-x-set! obj 100)

(pp obj)

More information about the Gambit-list mailing list