[racket] strange behavior with class/c, class, and define/contract---user error or Racket bug?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Jun 23 13:33:28 EDT 2013

On Jun 23, 2013, at 1:05 PM, Christopher wrote:

> However, this does NOT work as expected (changed class/c keyword from "field" to "init"):
> #lang racket
> (define/contract my-string%
>   (class/c (init [string-thing string?]))
>   (class object%
>     (super-new)))
> (print (new my-string%))
> [running the code produces the following:]
> Welcome to DrRacket, version 5.3.4 [3m].
> Language: racket; memory limit: 128 MB.
> (object:my-string% ...)
> > 
> In other words, when I declare an initialization parameter in the class contract which is absent from the class declaration and is not inherited, I expect a contract violation exception to be raised, similar to the one above for the non-existant field, either on execution of the define/contract clause or later when I try to instantiate the class with new.  However, Racket does not complain at all---at neither location.
> If Racket does not check that init parameters declared in class/c are actually present in the class itself or one of its superclasses, why does the class/c form allow them to be declared at all?  Is it just for documentation?
> I appreciate the feedback.  Thanks!

init contract clauses are useful to check the initial value: 

#lang racket

(define/contract my-string%
  (class/c (init [string-thing string?]))
  (class object%
    (init string-thing)

(print (new my-string% (string-thing 10)))

As it turns out, the class initialization system is so dynamic in Racket that it is impossible to decide whether a class has an init parameter or an init-field. 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130623/887679fa/attachment.html>

Posted on the users mailing list.