[racket] Maybe a silly question: Why racket don't have a constructor?

From: Danny Yoo (dyoo at hashcollision.org)
Date: Wed Dec 19 23:32:09 EST 2012

On Wed, Dec 19, 2012 at 5:07 PM, Chen Xiao <chenxiao770117 at gmail.com> wrote:
> Why racket's class not like Java have a constructor method?


The body of the class can do things that a constructor method can
typically do, including calling the instantiation for the super class.

Example:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket

(define thing%
  (class object%
    (init-field name)
    (super-new)
    (printf "making up ~a\n" name)))

(define food%
  (class thing%
    (init-field calories)
    (inherit-field name)

    (printf "bubble, bubble...\n")
    (super-new)
    (printf "Cooking ~a (~a cals)\n" name calories)))

(define t (new thing%
               [name "rainbow"]))
(define f (new food%
               [name "ramen"]
               [calories 56000]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


The calls to 'super-new' there are calls to the parent class's initializer code.

Does this do what you want?

Posted on the users mailing list.