6.3 Creating Objects

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

make-object
new
instantiate
dynamic-instantiate
super-make-object
super-instantiate
super-new

top contents ← prev up next →

### 6.3 Creating Objects🔗ℹ

The make-object procedure creates a new object with by-position initialization arguments, the new form creates a new object with by-name initialization arguments, and the instantiate form creates a new object with both by-position and by-name initialization arguments.

All fields in the newly created object are initially bound to the special #<undefined> value (see Void). Initialization variables with default value expressions (and no provided value) are also initialized to #<undefined>. After argument values are assigned to initialization variables, expressions in field clauses, init-field clauses with no provided argument, init clauses with no provided argument, private field definitions, and other expressions are evaluated. Those expressions are evaluated as they appear in the class expression, from left to right.

Sometime during the evaluation of the expressions, superclass-declared initializations must be evaluated once by using the super-make-object procedure, super-new form, or super-instantiate form.

By-name initialization arguments to a class that have no matching initialization variable are implicitly added as by-name arguments to a super-make-object, super-new, or super-instantiate invocation, after the explicit arguments. If multiple initialization arguments are provided for the same name, the first (if any) is used, and the unused arguments are propagated to the superclass. (Note that converted by-position arguments are always placed before explicit by-name arguments.) The initialization procedure for the object% class accepts zero initialization arguments; if it receives any by-name initialization arguments, then exn:fail:object exception is raised.

If the end of initialization is reached for any class in the hierarchy without invoking the superclass’s initialization, the exn:fail:object exception is raised. Also, if superclass initialization is invoked more than once, the exn:fail:object exception is raised.

Fields inherited from a superclass are not initialized until the superclass’s initialization procedure is invoked. In contrast, all methods are available for an object as soon as the object is created; the overriding of methods is not affected by initialization (unlike objects in C++).

> procedure (make-object class init-v ...) → object?
> class : class?
> init-v : any/c

Creates an instance of class. The init-vs are passed as initialization arguments, bound to the initialization variables of class for the newly created object as described in Initialization Variables. If class is not a class, the exn:fail:contract exception is raised.

> syntax (new class-expr (id by-name-expr) ...)

Creates an instance of the value of class-expr (which must be a class), and the value of each by-name-expr is provided as a by-name argument for the corresponding id.

> syntax (instantiate class-expr (by-pos-expr ...) (id by-name-expr) ...)

Creates an instance of the value of class-expr (which must be a class), and the values of the by-pos-exprs are provided as by-position initialization arguments. In addition, the value of each by-name-expr is provided as a by-name argument for the corresponding id.

> procedure (dynamic-instantiate cls pos-vs named-vs) → object?
> cls : class?
> pos-vs : list?
> named-vs : (listof (cons/c symbol? any/c))

Like (apply make-object cls pos-vs), but named-vs supplies named arguments in addition to the by-position arguments supplied by pos-vs.

Examples:

> > (define point% (class object%
> (super-new)
> (init-field x y)))
> > (define p (dynamic-instantiate point% '(1) '([y . 2])))
> > (get-field x p)
> 1
> > (get-field y p)
> 2

Added in version 8.8.0.1 of package base.

> syntax super-make-object

Produces a procedure that takes by-position arguments an invokes superclass initialization. See Creating Objects for more information.

> syntax (super-instantiate (by-pos-expr ...) (id by-expr ...) ...)

Invokes superclass initialization with the specified by-position and by-name arguments. See Creating Objects for more information.

> syntax (super-new (id by-name-expr ...) ...)

Invokes superclass initialization with the specified by-name arguments. See Creating Objects for more information.
top contents ← prev up next →