34 Interface-Oriented Programming for Classes

▼ |  | Unstable

1 | Guidelines for developing unstable libraries
2 | Bytes
3 | Classes and Objects
4 | Contracts
5 | Definitions
6 | Dictionaries
7 | Directories
8 | Exceptions
9 | Filesystem
10 | Functions
11 | Lists
12 | Net
13 | Path
14 | PLane T Packages
15 | Ports
16 | Pretty-Printing
17 | Regular Expressions
18 | Requiring Modules
19 | Sandboxed Evaluation
20 | Scribble Documentation
21 | Sets
22 | S-Expression Diff
23 | Source Locations
24 | Strings
25 | Structs
26 | Syntax
27 | Planet Package Macros
28 | Text Representations
29 | Multiple Values
30 | XML and CSS
31 | Polymorphic Contracts
32 | Finding Mutated Variables
33 | Find
34 | Interface-Oriented Programming for Classes
35 | Sequences
36 | Hash Tables
37 | Match
38 | Generics
39 | Mark Parameters
40 | Debugging
41 | Contracts for macro subexpressions
42 | GUI libraries

On this page:

define-interface
define-interface/ dynamic
send/ i
send*/ i
send/ apply/ i
define/ i
init/ i
init-field/ i
init-private/ i

Version: 5.0.1.5
top← prev up next →

### 34`` Interface-Oriented Programming for Classes

Ryan Culpepper <ryanc@racket-lang.org>

( require unstable/class-iop)

This library is unstable; compatibility will not be maintained. See Unstable for more information.

( define-interface name-id (super-ifc-id ...) (method-id ...))

Defines name-id as a static interface extending the interfaces named by the super-ifc-ids and containing the methods specified by the method-ids.

A static interface name is used by the checked method call variants (send/i, send*/i, and send/apply/i). When used as an expression, a static interface name evaluates to an interface value.

Examples:
> ( define-interface stack<%> () (empty? push pop))
> stack<%>
#<|interface:stack<%>|>

> ( define stack%
( class* object% (stack<%>)
( define items null)
( define/public (empty?) ( null? items))
( define/public (push x) ( set! items ( cons x items)))
( define/public (pop) ( begin ( car items) ( set! items ( cdr items))))
( super-new)))

( define-interface/dynamic name-id ifc-expr (method-id ...))

Defines name-id as a static interface with dynamic counterpart ifc-expr, which must evaluate to an interface value. The static interface contains the methods named by the method-ids. A run-time error is raised if any method-id is not a member of the dynamic interface ifc-expr.

Use define-interface/dynamic to wrap interfaces from other sources.

Examples:
> ( define-interface/dynamic object<%> ( class->interface object%) ())
> object<%>
#<interface:object%>

( send/i obj-exp static-ifc-id method-id arg-expr ...)

Checked variant of send.

The argument static-ifc-id must be defined as a static interface. The method method-id must be a member of the static interface static-ifc-id; otherwise a compile-time error is raised.

The value of obj-expr must be an instance of the interface static-ifc-id; otherwise, a run-time error is raised.

Examples:
> ( define s ( new stack%))
> ( send/i s stack<%> push 1)
> ( send/i s stack<%> popp)
eval:9:0: send/i: method not in static interface in: popp
> ( send/i ( new object%) stack<%> push 2)
send/i: interface check failed on: (object)

( send*/i obj-expr static-ifc-id (method-id arg-expr ...) ...)

Checked variant of send*.

Example:

> ( send*/i s stack<%>
(push 2)
(pop))

( send/apply/i obj-expr static-ifc-id method-id arg-expr ... list-arg-expr)

Checked variant of send/apply.

Example:
> ( send/apply/i s stack<%> push ( list 5))

( define/i id static-ifc-id expr)

Checks that expr evaluates to an instance of static-ifc-id before binding it to id. If id is subsequently changed (with set!), the check is performed again.

No dynamic object check is performed when calling a method (using send/i, etc) on a name defined via define/i.

( init/i (id static-ifc-id maybe-default-expr) ...)

( init-field/i (id static-ifc-id maybe-default-expr) ...)

( init-private/i (id static-ifc-id maybe-default-expr) ...)

maybe-default-expr |  | = |  | ()
|  | | |  | default-expr

Checked versions of init and init-field. The value attached to each id is checked against the given interface.

No dynamic object check is performed when calling a method (using send/i, etc) on a name bound via one of these forms. Note that in the case of init-field/i this check omission is unsound in the presence of mutation from outside the class. This should be fixed.

top← prev up next →