On this page:
object-contract
mixin-contract
make-mixin-contract
is-a?/ c
implementation?/ c
subclass?/ c
Version: 4.1

5.7 Object and Class Contracts

(object-contract member-spec ...)

 

member-spec

 

=

 

(method-id method-contract)

 

 

|

 

(field field-id contract-expr)

 

 

 

 

 

method-contract

 

=

 

(-> dom ... range)

 

 

|

 

(->* (mandatory-dom ...)

     (optional-dom ...)

     rest

     range)

 

 

|

 

(->d (mandatory-dependent-dom ...)

     (optional-dependent-dom ...)

     dependent-rest

     pre-cond

     dep-range)

 

 

 

 

 

dom

 

=

 

dom-expr

 

 

|

 

keyword dom-expr

 

 

 

 

 

range

 

=

 

range-expr

 

 

|

 

(values range-expr ...)

 

 

|

 

any

 

 

 

 

 

mandatory-dom

 

=

 

dom-expr

 

 

|

 

keyword dom-expr

 

 

 

 

 

optional-dom

 

=

 

dom-expr

 

 

|

 

keyword dom-expr

 

 

 

 

 

rest

 

=

 

 

 

|

 

#:rest rest-expr

 

 

 

 

 

mandatory-dependent-dom

 

=

 

[id dom-expr]

 

 

|

 

keyword [id dom-expr]

 

 

 

 

 

optional-dependent-dom

 

=

 

[id dom-expr]

 

 

|

 

keyword [id dom-expr]

 

 

 

 

 

dependent-rest

 

=

 

 

 

|

 

#:rest id rest-expr

 

 

 

 

 

pre-cond

 

=

 

 

 

|

 

#:pre-cond boolean-expr

 

 

 

 

 

dep-range

 

=

 

any

 

 

|

 

[id range-expr] post-cond

 

 

|

 

(values [id range-expr] ...) post-cond

 

 

 

 

 

post-cond

 

=

 

 

 

|

 

#:post-cond boolean-expr

Produces a contract for an object.

Each of the contracts for a method has the same semantics as the corresponding function contract, but the syntax of the method contract must be written directly in the body of the object-contract – much like the way that methods in class definitions use the same syntax as regular function definitions, but cannot be arbitrary procedures. The only exception is that ->d contracts implicitly bind this to the object itself.

mixin-contract : contract?

A function contract that recognizes mixins. It guarantees that the input to the function is a class and the result of the function is a subclass of the input.

(make-mixin-contract type ...)  contract?

  type : (or/c class? interface?)

Produces a function contract that guarantees the input to the function is a class that implements/subclasses each type, and that the result of the function is a subclass of the input.

(is-a?/c type)  flat-contract?

  type : (or/c class? interface?)

Accepts a class or interface and returns a flat contract that recognizes objects that instantiate the class/interface.

(implementation?/c interface)  flat-contract?

  interface : interface?

Returns a flat contract that recognizes classes that implement interface.

(subclass?/c class)  flat-contract?

  class : class?

Returns a flat-contract that recognizes classes that are subclasses of class.