[plt-scheme] PLT Object System

From: David J. Neu (djneu at att.net)
Date: Sun Feb 20 11:19:57 EST 2005


I have a question about the PLT object system ...

It's easy to parametrize the behavior of a procedure as
shown below:

(define f
  (lambda (x . g)
    (if (null? g)
        (add1 x)
        ((car g) x))))

(display (f 2)) (newline)
(display (f 2 (lambda (x) (* x x)))) (newline)

Now similarly, suppose you'd like to define a class to have a GET and
a SET procedure for a variable, and that you'd like to be able to
"override" these procedures on a one-time basis without bothering to
derive a new class.

The example below works up until the last line:

(require (lib "class.ss"))

 (define foo%
   (class object%
     (init-field x
                 (get (lambda () x))
                 (set (lambda (_x) (set! x _x))))
     (set x)

(define bar (new foo% (x 2)))
(display (get-field x bar)) (newline)

(define barbar (new foo% (x 2) (get (lambda () (add1 x)))))
(display (send get x barbar)) (newline) ;; FAILS - no GET method

It seems that their is a differentiation between fields and methods
that does not exist in "the rest" of Scheme, so the idiom used to
parametrize a procedure in my first example doesn't work.

Is there a way to do this?

Do generics have a role?

Many thanks!


Posted on the users mailing list.