6.4.1 Methods
6.4.2 Fields
6.4.3 Generics
On this page:
6.4.1Methods
6.4.2Fields
6.4.3Generics
top
up

6.4Field and Method AccessπŸ”— i

In expressions within a class definition, the initialization variables, fields, and methods of the class are all part of the environment. Within a method body, only the fields and other methods of the class can be referenced; a reference to any other class-introduced identifier is a syntax error. Elsewhere within the class, all class-introduced identifiers are available, and fields and initialization variables can be mutated with set! .

6.4.1MethodsπŸ”— i

Method names used within a class can only be used in the procedure position of an application expression; any other use is a syntax error.

To allow methods to be applied to lists of arguments, a method application can have the following form:

(method-idarg.... arg-list-expr)

This form calls the method in a way analogous to (apply method-idarg... arg-list-expr). The arg-list-expr must not be a parenthesized expression.

Methods are called from outside a class with the send , send/apply , and send/keyword-apply forms.

syntax

( send obj-exprmethod-idarg...)

(send obj-exprmethod-idarg.... arg-list-expr)
Evaluates obj-expr to obtain an object, and calls the method with (external) name method-id on the object, providing the arg results as arguments. Each arg is as for #%app : either arg-expr or keywordarg-expr. In the second form, arg-list-expr cannot be a parenthesized expression.

If obj-expr does not produce an object, the exn:fail:contract exception is raised. If the object has no public method named method-id, the exn:fail:object exception is raised.

syntax

( send/apply obj-exprmethod-idarg...arg-list-expr)

Like the dotted form of send , but arg-list-expr can be any expression.

syntax

( send/keyword-apply obj-exprmethod-id
keyword-list-exprvalue-list-expr
arg...arg-list-expr)
Like send/apply , but with expressions for keyword and argument lists like keyword-apply .

procedure

( dynamic-send obj
method-name
v...
#:<kw>kw-arg...)any
obj:object?
method-name:symbol?
v:any/c
kw-arg:any/c
Calls the method on obj whose name matches method-name, passing along all given vs and kw-args.

syntax

( send* obj-exprmsg...+)

msg = (method-idarg...)
| (method-idarg.... arg-list-expr)
Calls multiple methods (in order) of the same object. Each msg corresponds to a use of send .

For example,

(send* edit(begin-edit-sequence)
(insert"Hello")
(insert#\newline)
(end-edit-sequence))

is the same as

(let ([oedit])
(send obegin-edit-sequence)
(send oinsert"Hello")
(send oinsert#\newline)
(send oend-edit-sequence))

syntax

( send+ obj-exprmsg...)

msg = (method-idarg...)
| (method-idarg.... arg-list-expr)
Calls methods (in order) starting with the object produced by obj-expr. Each method call will be invoked on the result of the last method call, which is expected to be an object. Each msg corresponds to a use of send .

This is the functional analogue of send* .

Examples:
(define point%
(init-field [x0][y0])
(define/public (move-xdx)
(new this% [x(+ xdx)][yy]))
(define/public (move-ydy)
(new this% [y(+ ydy)][xx]))
(define/public (get-pair)
(cons xy))))
> (send+ (new point%)
(move-x5)
(move-y7)
(move-x12)
(get-pair))

'(17 . 7)

syntax

( with-method ([id(obj-exprmethod-id)]...)
body...+)
Extracts methods from an object and binds a local name that can be applied directly (in the same way as declared methods within a class) for each method. Each obj-expr must produce an object, which must have a public method named by the corresponding method-id. The corresponding id is bound so that it can be applied directly (see Methods).

Example:

(let ([s(new stack%)])
(with-method ([push(spush!)]
[pop(spop!)])
(push10)
(push9)
(pop)))

is the same as

(let ([s(new stack%)])
(send spush!10)
(send spush!9)
(send spop!))

6.4.2FieldsπŸ”— i

syntax

( get-field idobj-expr)

Extracts the field with (external) name id from the value of obj-expr.

If obj-expr does not produce an object, the exn:fail:contract exception is raised. If the object has no id field, the exn:fail:object exception is raised.

procedure

( dynamic-get-field field-nameobj)any/c

field-name:symbol?
obj:object?
Extracts the field from obj with the (external) name that matches field-name. If the object has no field matching field-name, the exn:fail:object exception is raised.

syntax

( set-field! idobj-exprexpr)

Sets the field with (external) name id from the value of obj-expr to the value of expr.

If obj-expr does not produce an object, the exn:fail:contract exception is raised. If the object has no id field, the exn:fail:object exception is raised.

procedure

( dynamic-set-field! field-nameobjv)void?

field-name:symbol?
obj:object?
v:any/c
Sets the field from obj with the (external) name that matches field-name to v. If the object has no field matching field-name, the exn:fail:object exception is raised.

syntax

( field-bound? idobj-expr)

Produces #t if the object result of obj-expr has a field with (external) name id, #f otherwise.

If obj-expr does not produce an object, the exn:fail:contract exception is raised.

syntax

( class-field-accessor class-exprfield-id)

Returns an accessor procedure that takes an instance of the class produced by class-expr and returns the value of the object’s field with (external) name field-id.

If class-expr does not produce a class, the exn:fail:contract exception is raised. If the class has no field-id field, the exn:fail:object exception is raised.

syntax

( class-field-mutator class-exprfield-id)

Returns a mutator procedure that takes an instance of the class produced by class-expr and a value, and sets the value of the object’s field with (external) name field-id to the given value. The result is #<void>.

If class-expr does not produce a class, the exn:fail:contract exception is raised. If the class has no field-id field, the exn:fail:object exception is raised.

6.4.3GenericsπŸ”— i

A generic can be used instead of a method name to avoid the cost of relocating a method by name within a class.

syntax

( generic class-or-interface-exprid)

Produces a generic that works on instances of the class or interface produced by class-or-interface-expr (or an instance of a class/interface derived from class-or-interface) to call the method with (external) name id.

If class-or-interface-expr does not produce a class or interface, the exn:fail:contract exception is raised. If the resulting class or interface does not contain a method named id, the exn:fail:object exception is raised.

syntax

( send-generic obj-exprgeneric-exprarg...)

(send-generic obj-exprgeneric-exprarg.... arg-list-expr)
Calls a method of the object produced by obj-expr as indicated by the generic produced by generic-expr. Each arg is as for #%app : either arg-expr or keywordarg-expr. The second form is analogous to calling a procedure with apply , where arg-list-expr is not a parenthesized expression.

If obj-expr does not produce an object, or if generic-expr does not produce a generic, the exn:fail:contract exception is raised. If the result of obj-expr is not an instance of the class or interface encapsulated by the result of generic-expr, the exn:fail:object exception is raised.

procedure

( make-generic typemethod-name)generic?

method-name:symbol?
Like the generic form, but as a procedure that accepts a symbolic method name.

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /