[plt-scheme] field contracts?

From: Robby Findler (robby at cs.uchicago.edu)
Date: Sat Oct 9 13:34:45 EDT 2004

Oh, we don't have class contracts yet. It's not clear how to properly
protect super-classes from subclasses.

The other operation on classes is instance creation and we can have
contracts there, but you'd have to do something like this:

  (module m mzscheme
    (provide/contract [create (-> (object-contract ...))])
    (define (create) (new point%))
    ...)

That is, create a function that returns instances.

Robby

At Sat,  9 Oct 2004 10:30:28 -0700 (PDT), Michael Vanier wrote:
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> 
> This is nice.  I'm having trouble using provide/contract with classes:
> 
>   (module m mzscheme
>     (require (lib "contract.ss") (lib "class.ss"))
>     (provide/contract
>      (point% (object-contract (field x number?) (field y number?))))
>   
>     (define point%
>       (class* object% ()
>         (public set-x! set-y!)
>         (field (x 0)) 
>         (field (y 0))
>         (define (set-x! new-x) (set! x new-x))
>         (define (set-y! new-y) (set! y new-y))
>         (super-new))))
> 
>   (require m)
>   (define p1 (make-object point%))
> 
> gives:
> 
>   top-level: m broke point%'s contract: 
>   (object-contract (field x number?) (field y number?)): 
>   expected an object, got #<struct:class:point%>
> 
> This makes sense, but how do I tell provide/contract that point% is a
> class, not an object?
> 
> Mike
> 
> 
> > From: Robby Findler <robby at cs.uchicago.edu>
> > Cc: plt-scheme at list.cs.brown.edu
> > Date: Sat, 9 Oct 2004 09:54:06 -0500
> > 
> > Here's one way to do that:
> > 
> > (module m mzscheme
> >   (provide/contract
> >     (struct posn ((x number?) (y number?))))
> > 
> >   (define-struct posn (x y)))
> > 
> > Robby
> > 
> > At Sat,  9 Oct 2004 01:06:14 -0700 (PDT), Michael Vanier wrote:
> > >   For list-related administrative tasks:
> > >   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> > > 
> > > 
> > > Quick question:
> > > 
> > > Is there a reasonably straightforward way to define a struct or object type
> > > whose fields are required to be of specific types?  It seems like the
> > > contract system should be usable for this, but I don't see any examples in
> > > the PLT scheme documentation.
> > > 
> > > Mike
> > 


Posted on the users mailing list.