On this page:
interface
interface*

5.1 Creating Interfaces

+Classes and Objects in The Racket Guide introduces classes, objects, and interfaces.

syntax

(interface (super-interface-expr ...) name-clause ...)

 
name-clause = id
  | (id contract-expr)
Produces an interface. The ids must be mutually distinct.

Each super-interface-expr is evaluated (in order) when the interface expression is evaluated. The result of each super-interface-expr must be an interface value, otherwise the exn:fail:object exception is raised. The interfaces returned by the super-interface-exprs are the new interface’s superinterfaces, which are all extended by the new interface. Any class that implements the new interface also implements all of the superinterfaces.

The result of an interface expression is an interface that includes all of the specified ids, plus all identifiers from the superinterfaces. Duplicate identifier names among the superinterfaces are ignored, but if a superinterface contains one of the ids in the interface expression, the exn:fail:object exception is raised. A given id may be paired with a corresponding contract-expr.

If no super-interface-exprs are provided, then the derivation requirement of the resulting interface is trivial: any class that implements the interface must be derived from object%. Otherwise, the implementation requirement of the resulting interface is the most specific requirement from its superinterfaces. If the superinterfaces specify inconsistent derivation requirements, the exn:fail:object exception is raised.

Examples:

(define file-interface<%>
  (interface () open close read-byte write-byte))
(define directory-interface<%>
  (interface (file-interface<%>)
    [file-list (-> (listof (is-a?/c file-interface<%>)))]
    parent-directory))

syntax

(interface* (super-interface-expr ...)
            ([property-expr val-expr] ...)
  name-clause ...)
 
name-clause = id
  | (id contract-expr)
Like interface, but also associates to the interface the structure-type properties produced by the property-exprs with the corresponding val-exprs.

Whenever the resulting interface (or a sub-interface derived from it) is explicitly implemented by a class through the class* form, each property is attached with its value to a structure type that instantiated by instances of the class. Specifically, the property is attached to a structure type with zero immediate fields, which is extended to produce the internal structure type for instances of the class (so that no information about fields is accessible to the structure type property’s guard, if any).

Example:

(define i<%> (interface* () ([prop:custom-write
                              (lambda (obj port mode) (void))])
               method1 method2 method3))