8.18
top
← prev up next →

Interface-Oriented Programming for ClassesπŸ”— i

Ryan Culpepper <ryanc@racket-lang.org>

syntax

( 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?pushpop))
> stack<%>

#<interface:stack<%>>

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

syntax

( define-interface/dynamic name-idifc-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:
> object<%>

#<interface:object%>

syntax

( send/i obj-expstatic-ifc-idmethod-idarg-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 sstack<%>push1)
> (send/i sstack<%>popp)

eval:9:0: send/i: method not in static interface

in: popp

> (send/i (new object% )stack<%>push2)

send/i: interface check failed on: (object)

syntax

( send*/i obj-exprstatic-ifc-id(method-idarg-expr...)...)

Checked variant of send* .

Example:
> (send*/i sstack<%>
(push2)
(pop))

syntax

( send/apply/i obj-exprstatic-ifc-idmethod-idarg-expr...list-arg-expr)

Checked variant of send/apply .

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

syntax

( define/i idstatic-ifc-idexpr)

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 .

syntax

( init/i (idstatic-ifc-idmaybe-default-expr)...)

syntax

( init-field/i (idstatic-ifc-idmaybe-default-expr)...)

syntax

( init-private/i (idstatic-ifc-idmaybe-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.

syntax

( define-interface-expander idtransformer-expr)

Defines id as a macro that can be used within define-interface forms.

Examples:
> (define-interface-expander stack-methods
(lambda (stx)#'[empty?pushpop]))
> (define-interface stack<%>()
((stack-methods)))

'(empty? push pop)

top
← prev up next →

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