► |  | 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:

object?
class?
interface?
generic?
object=?
object->vector
class->interface
object-interface
is-a?
subclass?
implementation?
interface-extension?
method-in-interface?
interface->method-names
object-method-arity-includes?
field-names
object-info
class-info
exn: fail: object

top ← prev up next →

#### 6.11`` Object, Class, and Interface Utilities

> procedure (object? v) → boolean?
> v : any/c

Returns #t if v is an object, #f otherwise.

Examples:
> > (object? (new object%))
> #t
> > (object? object%)
> #f
> > (object? "clam chowder")
> #f

> procedure (class? v) → boolean?
> v : any/c

Returns #t if v is a class, #f otherwise.

Examples:
> > (class? object%)
> #t
> > (class? (class object% (super-new)))
> #t
> > (class? (new object%))
> #f
> > (class? "corn chowder")
> #f

> procedure (interface? v) → boolean?
> v : any/c

Returns #t if v is an interface, #f otherwise.

Examples:
> > (interface? (interface () empty cons first rest))
> #t
> > (interface? object%)
> #f
> > (interface? "gazpacho")
> #f

> procedure (generic? v) → boolean?
> v : any/c

Returns #t if v is a generic, #f otherwise.

Examples:
> (define c%
> (class object%
> (super-new)
> (define/public (m x)
> (+ 3.14 x))))
> > (generic? (generic c% m))
> #t
> > (generic? c%)
> #f
> > (generic? "borscht")
> #f

> procedure (object=? a b) → boolean?
> a : object?
> b : object?

Determines if two objects are the same object, or not; this procedure uses eq?, but also works properly with contracts.

Examples:
> (define obj-1 (new object%))
> > (define/contract obj-2 (object/c) obj-1)
> > (object=? obj-1 obj-1)
> #t
> > (object=? (new object%) obj-1)
> #f
> > (object=? obj-1 obj-2)
> #t
> > (object=? obj-1 (new (class object% (super-new))))
> #f

> procedure (object->vector object [opaque-v]) → vector?
> object : object?
> opaque-v : any/c = #f

Returns a vector representing object that shows its inspectable fields, analogous to struct->vector.

Examples:
> > (object->vector (new object%))
> '#(object:object% ...)
> > (object->vector (new (class object%
> (super-new)
> (field [x 5] [y 10]))))
> '#(object:eval:106:0 ...)

> procedure (class->interface class) → interface?
> class : class?

Returns the interface implicitly defined by class.

Example:
> > (class->interface object%)
> #<interface:object%>

> procedure (object-interface object) → interface?
> object : object?

Returns the interface implicitly defined by the class of object.

Example:
> > (object-interface (new object%))
> #<interface:object%>

> procedure (is-a? v type) → boolean?
> v : any/c
> type : (or/c interface? class?)

Returns #t if v is an instance of a class type or a class that implements an interface type, #f otherwise.

Examples:
> (define point<%> (interface () get-x get-y))
> (define 2d-point%
> (class* object% (point<%>)
> (super-new)
> (field [x 0] [y 0])
> (define/public (get-x) x)
> (define/public (get-y) y)))
> > (is-a? (new 2d-point%) 2d-point%)
> #t
> > (is-a? (new 2d-point%) point<%>)
> #t
> > (is-a? (new object%) 2d-point%)
> #f
> > (is-a? (new object%) point<%>)
> #f

> procedure (subclass? v cls) → boolean?
> v : any/c
> cls : class?

Returns #t if v is a class derived from (or equal to) cls, #f otherwise.

Examples:
> > (subclass? (class object% (super-new)) object%)
> #t
> > (subclass? object% (class object% (super-new)))
> #f
> > (subclass? object% object%)
> #t

> procedure (implementation? v intf) → boolean?
> v : any/c
> intf : interface?

Returns #t if v is a class that implements intf, #f otherwise.

Examples:
> (define i<%> (interface () go))
> (define c%
> (class* object% (i<%>)
> (super-new)
> (define/public (go) 'go)))
> > (implementation? c% i<%>)
> #t
> > (implementation? object% i<%>)
> #f

> procedure (interface-extension? v intf) → boolean?
> v : any/c
> intf : interface?

Returns #t if v is an interface that extends intf, #f otherwise.

Examples:
> (define point<%> (interface () get-x get-y))
> (define colored-point<%> (interface (point<%>) color))
> > (interface-extension? colored-point<%> point<%>)
> #t
> > (interface-extension? point<%> colored-point<%>)
> #f
> > (interface-extension? (interface () get-x get-y get-z) point<%>)
> #f

> procedure (method-in-interface? sym intf) → boolean?
> sym : symbol?
> intf : interface?

Returns #t if intf (or any of its ancestor interfaces) includes a member with the name sym, #f otherwise.

Examples:
> (define i<%> (interface () get-x get-y))
> > (method-in-interface? 'get-x i<%>)
> #t
> > (method-in-interface? 'get-z i<%>)
> #f

> procedure (interface->method-names intf) → (listof symbol?)
> intf : interface?

Returns a list of symbols for the method names in intf, including methods inherited from superinterfaces, but not including methods whose names are local (i.e., declared with define-local-member-name).

Examples:
> (define i<%> (interface () get-x get-y))
> > (interface->method-names i<%>)
> '(get-x get-y)

> > procedure
> > (object-method-arity-includes? |  | object |  |  |  |
> > |  | sym |  |  |  |
> > |  | cnt) |  | → |  | boolean?
> object : object?
> sym : symbol?
> cnt : exact-nonnegative-integer?

Returns #t if object has a method named sym that accepts cnt arguments, #f otherwise.

Examples:
> (define c%
> (class object%
> (super-new)
> (define/public (m x [y 0])
> (+ x y))))
> > (object-method-arity-includes? (new c%) 'm 1)
> #t
> > (object-method-arity-includes? (new c%) 'm 2)
> #t
> > (object-method-arity-includes? (new c%) 'm 3)
> #f
> > (object-method-arity-includes? (new c%) 'n 1)
> #f

> procedure (field-names object) → (listof symbol?)
> object : object?

Returns a list of all of the names of the fields bound in object, including fields inherited from superinterfaces, but not including fields whose names are local (i.e., declared with define-local-member-name).

Examples:
> > (field-names (new object%))
> '()
> > (field-names (new (class object% (super-new) (field [x 0] [y 0]))))
> '(x y)

> > procedure
> > (object-info object) |  | → |  |
> > (or/c class? #f) |  | boolean?
> object : object?

Returns two values, analogous to the return values of struct-info:

- class: a class or #f; the result is #f if the current inspector does not control any class for which the object is an instance.

- skipped?: #f if the first result corresponds to the most specific class of object, #t otherwise.

> procedure (class-info class)
> | → |  |
> symbol?
> exact-nonnegative-integer?
> (listof symbol?)
> (any/c exact-nonnegative-integer? . -> . any/c)
> (any/c exact-nonnegative-integer? any/c . -> . any/c)
> (or/c class? #f)
> boolean?
> class : class?

Returns seven values, analogous to the return values of struct-type-info:

- name: the class’s name as a symbol;

- field-cnt: the number of fields (public and private) defined by the class;

- field-name-list: a list of symbols corresponding to the class’s public fields; this list can be larger than field-cnt because it includes inherited fields;

- field-accessor: an accessor procedure for obtaining field values in instances of the class; the accessor takes an instance and a field index between 0 (inclusive) and field-cnt (exclusive);

- field-mutator: a mutator procedure for modifying field values in instances of the class; the mutator takes an instance, a field index between 0 (inclusive) and field-cnt (exclusive), and a new field value;

- super-class: a class for the most specific ancestor of the given class that is controlled by the current inspector, or #f if no ancestor is controlled by the current inspector;

- skipped?: #f if the sixth result is the most specific ancestor class, #t otherwise.

> > struct
> > (struct |  | exn:fail:object exn:fail ()
> > |  | #:extra-constructor-name make-exn:fail:object)

Raised for class-related failures, such as attempting to call a method that is not supplied by an object.
top ← prev up next →