6.11 Object, Class, and Interface Utilities
> (class? object%) #t
> (class? (class object% (super-new))) #t
> (class? (new object%)) #f
> (class? "corn chowder") #f
procedure
(interface? v) → boolean?
v : any/c
> (interface? (interface () empty cons first rest)) #t
> (interface? object%) #f
> (interface? "gazpacho") #f
> (define c% (class object% (super-new) (define/public (m x) (+ 3.14 x))))
> (generic? (generic c% m)) #t
> (generic? c%) #f
> (generic? "borscht") #f
This procedure is similar in spirit to eq? but also works properly with contracts (and has a stronger guarantee).
> (define obj-1 (new object%))
> (define obj-2 (new object%))
> (define/contract obj-3 (object/c) obj-1)
> (object=? obj-1 obj-1) #t
> (object=? obj-1 obj-2) #f
> (object=? obj-1 obj-3) #t
> (eq? obj-1 obj-1) #t
> (eq? obj-1 obj-2) #f
> (eq? obj-1 obj-3) #f
> (object-or-false=? #f (new object%)) #f
> (object-or-false=? (new object%) #f) #f
> (object-or-false=? #f #f) #t
Added in version 6.1.1.8 of package base.
procedure
(object->vector object [opaque-v]) → vector?
object : object? opaque-v : any/c = #f
> (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?
> (class->interface object%) #<interface:object%>
procedure
(object-interface object) → interface?
object : object?
> (object-interface (new object%)) #<interface:object%>
> (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
> (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?
> (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?
> (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?
> (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?
> (define i<%> (interface () get-x get-y))
> (interface->method-names i<%>) '(get-y get-x)
procedure
(object-method-arity-includes? object sym cnt) → boolean? object : object? sym : symbol? cnt : exact-nonnegative-integer?
> (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?
> (field-names (new object%)) '()
> (field-names (new (class object% (super-new) (field [x 0] [y 0])))) '(y x)
procedure
(object-info object) →
(or/c class? #f) boolean? object : object?
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?
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)
procedure
(class-seal class key unsealed-inits unsealed-fields unsealed-methods inst-proc member-proc) → class? class : class? key : symbol? unsealed-inits : (listof symbol?) unsealed-fields : (listof symbol?) unsealed-methods : (listof symbol?) inst-proc : (-> class? any) member-proc : (-> class? (listof symbol?) any)
When a class has any seals, the inst-proc procedure is called on instantiation (normally, this is used to raise an error on instantiation) and the member-proc function is called (again, this is normally used to raise an error) when a subclass attempts to add class members that are not listed in the unsealed lists.
The inst-proc is called with the class value on which an instantiation was attempted. The member-proc is called with the class value and the list of initialization argument, field, or method names.
procedure
(class-unseal class key wrong-key-proc) → class?
class : class? key : symbol? wrong-key-proc : (-> class? any)
If the unseal removed all of the seals in the class, the class value can be instantiated or subclassed freely. If the given class value does not contain or any seals or does not contain any seals with the given key, the wrong-key-proc function is called with the class value.