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)
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.
(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)
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).
(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.