5.4 Generic Interfaces
(require racket/generic) | package: base |
A generic interface allows per-type methods to be associated with generic functions. Generic functions are defined using a define-generics form. Method implementations for a structure type are defined using the #:methods keyword (see Defining Structure Types: struct).
syntax
(define-generics id generics-opt ... [method-id . kw-formals*] ... generics-opt ...)
generics-opt = #:defaults ([default-pred? default-impl ...] ...) | #:fast-defaults ([fast-pred? fast-impl ...] ...) | #:fallbacks [fallback-impl ...] | #:defined-predicate defined-pred-id | #:defined-table defined-table-id | #:derive-property prop-expr prop-value-expr kw-formals* = (arg* ...) | (arg* ...+ . rest-id) | rest-id arg* = arg-id | [arg-id] | keyword arg-id | keyword [arg-id]
gen:id as a transformer binding for the static information about a new generic interface;
id? as a predicate identifying instances of structure types that implement this generic group; and
each method-id as a generic method that calls the corresponding method on values where id? is true. Each method-id’s kw-formals* must include a required by-position argument that is free-identifier=? to id. That argument is used in the generic definition to locate the specialization.
id/c as a contract combinator that recognizes instances of structure types which implement the gen:id generic interface. The combinator takes pairs of method-ids and contracts. The contracts will be applied to each of the corresponding method implementations. The id/c combinator is intended to be used to contract the range of a constructor procedure for a struct type that implements the generic interface.
The #:defaults option may be provided at most once. When it is provided, each generic function uses default-pred?s to dispatch to the given default method implementations, default-impls, if dispatching to the generic method table fails. The syntax of the default-impls is the same as the methods provided for the #:methods keyword for struct.
The #:fast-defaults option may be provided at most once. It works the same as #:defaults, except the fast-pred?s are checked before dispatching to the generic method table. This option is intended to provide a fast path for dispatching to built-in datatypes, such as lists and vectors, that do not overlap with structures implementing gen:id.
The #:fallbacks option may be provided at most once. When it is provided, the fallback-impls define fallback method implementations that are used for any instance of the generic interface that does not supply a specific implementation. The syntax of the fallback-impls is the same as the methods provided for the #:methods keyword for struct.
The #:defined-predicate option may be provided at most once. When it is provided, defined-pred-id is defined as a procedure that reports whether a specific instance of the generic interface implements a given set of methods. Specifically, (defined-pred-id v 'name ...) produces #t if v has implementations for each method name, not counting #:fallbacks implementations, and produces #f otherwise. This procedure is intended for use by higher-level APIs to adapt their behavior depending on method availability.
The #:defined-table option may be provided at most once. When it is provided, defined-table-id is defined as a procedure that takes an instance of the generic interface and returns an immutable hash table that maps symbols corresponding to method names to booleans representing whether or not that method is implemented by the instance. This option is deprecated; use #:defined-predicate instead.
The #:derive-property option may be provided any number of times. Each time it is provided, it specifies a structure type property via prop-expr and a value for the property via prop-value-expr. All structures implementing the generic interface via #:methods automatically implement this structure type property using the provided values. When prop-value-expr is executed, each method-id is bound to its specific implementation for the structure type.
If a value v satisfies id?, then v is a generic instance of gen:id.
If a generic instance v has a corresponding implementation for some method-id provided via #:methods in struct or via #:defaults or #:fast-defaults in define-generics, then method-id is an implemented generic method of v.
If method-id is not an implemented generic method of a generic instance v, and method-id has a fallback implementation that does not raise an exn:fail:support exception when given v, then method-id is a supported generic method of v.
procedure
(raise-support-error name v) → none/c
name : symbol? v : any/c
Example: | |||
|
struct
(struct exn:fail:support exn:fail () #:transparent)
syntax
(define/generic local-id method-id)
local-id : identifier?
method-id : identifier?
Using the define/generic form outside a #:methods specification in struct (or define-struct) is an syntax error.
Examples: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
syntax
(generic-instance/c gen-id [method-id method-ctc] ...)
method-ctc : contract?
syntax
(impersonate-generics gen-id val-expr [method-id method-proc-expr] ... maybe-properties)
maybe-properties =
| #:properties props-expr
method-proc-expr : (any/c . -> . any/c)
props-expr : (list/c impersonator-property? any/c ... ...)
A props-expr can provide properties to attach to the impersonator. The result of props-expr bust be an list with an even number of elements, where the first element of the list is an impersonator property, the second element is its value, and so on.
Changed in version 6.1.1.8 of package base: Added #:properties.
syntax
(chaperone-generics gen-id val-expr [method-id method-proc-expr] ... maybe-properties)
syntax
(redirect-generics mode gen-id val-expr [method-id method-proc-expr] ... maybe-properties)