[plt-scheme] A puzzle for those people doing class.ss stuff

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Mon Nov 20 18:01:51 EST 2006

Here's a small puzzle:


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module test-class-scoping mzscheme
   (require (lib "class.ss"))
   (define f #f)

   (define some-class
     (class object%
       (super-new)
       (define/public (set-x arg) (void))
       (send this set-x 'muhahah)))

   (define (my-mixin super%)
     (class super%
       (super-new)

       (override set-x)
       (define x #f)

       (define (set-x new-x)
         (set! x new-x)
         (printf "x set to ~a~n" x))

       (define (look-at-x)
         (printf "x: ~s~n" x))

       (set! f look-at-x)))

   (define (test)
     (define my-instance
       (new (my-mixin some-class)))
     (f)
     (send my-instance set-x 42)
     (f)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Try predicting what TEST is going to print out.  It certainly caught me 
off guard.



(Backstory: I was chasing down a weird initialization bug in DivaScheme 
and traced it down to an unexpected timing interaction between defining 
things and calling SUPER-NEW.

So this is a potential pitfall for folks doing classes with class.ss.  Is 
there a way to prevent one of digging this kind of problem without 
resorting to idioms?  Thanks!)


Posted on the users mailing list.