[plt-scheme] field contracts?
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
>