Creating a Class in Racket

Creating a Class in Racket

Last updated:

Yeah. You know. Classes.

simplest possible class

(needs to inherit from the base object% class)

(define my-class%
  (class object%))

simplest possible class that can be instantiated

(needs to call the superclass constructor)

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

simplest class with a field

(field is called name)

(define my-class%
  (class object%
    (field (name 5))
    (super-new)))

class with a constructor for a field

send initial value as an argument for make-object

(define my-class%
  (class object%
    (init-field name)
    (super-new)))

class with a public method

(public method is called name-upper and returns the hardcoded name in uppercase)

(define my-class%
  (class object%
    (field (name "john"))
    (define/public (name-upper)
      (string-upcase name))
    (super-new)))

w.i.p.: This is a work in progress. I'm also learning.