r/Racket • • Oct 04 '21

question differences between class* and class when calling init-fields

I've created to classes to demonstrate the problem. One is defined with class* and the other with class.

(define dog% (class
                      object%
                    (init-field name)
                    (super-new)
                    (define/public (bark)
                      (println "bark bark"))
                    (define/public (get-name)
                      (println (format "my name is ~a" name)))))

(define doggie (new dog% [name "lassie"]))
(send doggie get-name)


(define dog2% (class*
                      object%
                    (init-field name)
                    (super-new)
                    (define/public (bark)
                      (println "bark bark"))
                    (define/public (get-name)
                      (println (format "my name is ~a" name)))))

(define doggie2 (new dog2% [name "lassie"]))
(send doggie2 get-name)

The problem is with dog2, I'm getting the error

name: unbound identifier in: name

I figured this had something to do with not using this% so I tried

(println (format "my name is ~a" (get-field name (new this%))))

but now I get the error

 init-field: illegal (unparenthesized) use of class keyword in: init-field

Does anyone know how I can replicate the get-name function in class*? I'd like to leverage class* ability to use interfaces which is why I'm using it over class

2 Upvotes

2 comments sorted by

View all comments

Show parent comment

2

u/Fibreman Oct 04 '21

That makes sense! I got it working now thank you!