[plt-scheme] ivar equivalent (circa class100.ss) available with current class system?

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Fri Aug 5 16:49:05 EDT 2005

Hi everyone,

I've been reading through some of the class material from readscheme.org,
and was curious why the IVAR function from the old class system doesn't
exist in the current class system:


The closest equivalents I could think of, initially, looked something like

  (define-syntax ivar
    (syntax-rules ()
      [(ivar inst-expr name)
       (let ((inst inst-expr))
         (with-method ((x (inst name)))

but I got the following error when trying it out:

> (require (lib "class.ss"))
> (define hello%
     (class object%
        (public say-hello)
        (define (say-hello name)
           (printf "hello ~s~%" name))
> (define my-instance (make-object hello%))
> (ivar my-instance say-hello)
repl-14:6:11: with-method: misuse of method (not in application) in: x

This seemed a bit unusual to me; it's a great error message, but I don't
see in the reference documentation why this restriction is enforced.  Is
it a deliberate design decision for with-method to prevent methods from
leaking out of their instances?

I did find a successful way to implement ivar-like behavior:

  (define-syntax ivar
    (syntax-rules ()
      [(ivar instance-expr method-name)
       (let* ((instance instance-expr)
               (lambda args
                 (send/apply instance method-name args))))
         ;; used the let to get inferred-name of the lambda
         ;; initialized properly

with the result:

> (ivar my-instance say-hello)
> ((ivar my-instance say-hello) 'danny)
hello danny

So I did finally figure out a way to do it, and I think it's working ok.
But I'm still feeling a little weird that the first approach didn't work,
and curious as to why IVAR was removed from the current class system.  So
more information on what motivated the changes between the old and new
class system design would be greatly appreciated.

Thank you!

Posted on the users mailing list.