3 Classes and Objects

▼ |  | 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

► | 3 | Classes and Objects

3.1 | Predicates and Contracts
3.2 | Mixins
3.3 | Methods

On this page:

3.1`` Predicates and Contracts
class-or-interface/ c
object-provides/ c
class-provides/ c
mixin-provides/ c
3.2`` Mixins
ensure-interface
3.3`` Methods
send+
send-each

Version: 5.0.1.5
top← prev up next →

### 3`` Classes and Objects

Carl Eastlund <cce@racket-lang.org>

( require unstable/class)

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

This module provides tools for classes, objects, and mixins.

#### 3.1`` Predicates and Contracts

class-or-interface/c : flat-contract?

Recognizes classes and interfaces.

( object-provides/c spec ...) → flat-contract?
spec : class-or-interface/c

Recognizes objects which are instances of all the given classes and interfaces.

( class-provides/c spec ...) → flat-contract?
spec : class-or-interface/c

Recognizes classes which are subclasses (not strictly) and implementations, respectively, of all the given classes and interfaces.

( mixin-provides/c [super-expr ...] [sub-expr ...])

Function contract for a mixin whose argument is the parent class c% matching ( class-provides/c super-expr ...) and whose result matches ( class-provides/c c% sub-expr ...).

#### 3.2`` Mixins

( ensure-interface i<%> mx c%) → ( class-provides/c c% i<%>)
i<%> : interface?
mx : ( mixin-provides/c [] [i<%>])
c% : class?

Returns c% if it implements i<%>; otherwise, returns (mx c%).

#### 3.3`` Methods

( send+ obj [message arg ...] ...)

Sends each message (with arguments) to obj, then returns obj.

Examples:

( define c%
( class object%
( super-new)
( define/public (say msg) ( printf "~a!\n" msg))))

> ( send+ ( new c%) [say 'Hello] [say 'Good-bye])

Hello!
Good-bye!

(object:c% ...)

( send-each objs message arg ...)

Sends the message to each object in the list objs, returning ( void).

Examples:

( define c%
( class object%
( super-new)
( init-field msg)
( define/public (say to) ( printf "~a, ~a!\n" msg to))))

> ( send-each
( list ( new c% [msg 'Hello])
( new c% [msg 'Good-bye]))
say 'World)

Hello, World!
Good-bye, World!

top← prev up next →