6.1 Creating Interfaces

► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

▼ | 6 | Classes and Objects

6.1 | Creating Interfaces
6.2 | Creating Classes
6.3 | Creating Objects
6.4 | Field and Method Access
6.5 | Mixins
6.6 | Traits
6.7 | Object and Class Contracts
6.8 | Object Equality and Hashing
6.9 | Object Serialization
6.10 | Object Printing
6.11 | Object, Class, and Interface Utilities
6.12 | Surrogates

On this page:

interface
interface*

Racket
top contents ← prev up next →

### 6.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)
> |  | | |  | (id #:public default-expr)
> |  | | |  | (id #:override default-expr)
> |  | | |  | (id contract-expr #:public impl-expr)
> |  | | |  | (id contract-expr #:override impl-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. A given id may be paired with a corresponding contract-expr, and it may have a impl-expr, which supplies an implementation of id to be inherited or overridden in an implementing class. Each impl-expr must be a method-procedure; see Method Definitions. Duplicate identifier names among the superinterfaces are ignored, as long as no more than one of them provides a default implementation for each identifier that originated in a different interface.

An interface can provide an implementation of a method using #:public if no superinterface has an implementation of the method, or using #:override otherwise. If multiple superinterfaces provide implementations of a method that originate from different ancestor interfaces, then the method must be overridden. The super form is not supported within an interface method implementation.

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, then exn:fail:object exception is raised is raised.

Examples:

> (define file-interface<%>
> (interface ()
> open close read-byte write-byte
> [append-line
> #:public
> (λ (bts)
> (send this open 'append)
> (for ([b (in-bytes bts)])
> (send this write-byte b))
> (send this close))]))
> (define directory-interface<%>
> (interface (file-interface<%>)
> [file-list (->m (listof (is-a?/c file-interface<%>)))]
> parent-directory))

Changed in version 8.17.0.4 of package base: Added support for #:public and #:override method implementations.

> > syntax
> > (interface* (super-interface-expr ...)
> > ([property-expr val-expr] ...)
> > name-clause ...)
> name-clause |  | = |  | id
> |  | | |  | (id contract-expr)
> |  | | |  | (id #:public default-expr)
> |  | | |  | (id #:override default-expr)
> |  | | |  | (id contract-expr #:public default-expr)
> |  | | |  | (id contract-expr #:override default-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))

Changed in version 8.17.0.4 of package base: Added support for #:public and #:override method implementations.

top contents ← prev up next →