2.1 Procedural Protocol
| |||||||||||||||||||||
make-signature : procedure? | |||||||||||||||||||||
default-signature : any/c = #f | |||||||||||||||||||||
ref-hierarchy : (-> hierarchy?) = global-hierarchy |
Creates a new multimethod. make-signature is the signature generator procedure applied every time the multimethod is called. default-signature is a default signature that is looked up in the method tables if the generated one doesn’t match any implementation. ref-hierarchy is a procedure yielding the value hierarchy to be used by signature comparisons for the new multimethod. ref-hierarchy defaults to global-hierarchy, ie. the default behaviour is to read the current value of the global hierarchy parameter every time the hierarchy is needed.
(multimethod? v) → boolean? |
v : any/c |
Checks whether the given object is a multimethod.
(multimehtod-make-signature m) → procedure? |
m : multimethod? |
Retrieves the signature generator of a multimethod.
(multimethod-default-signature m) → any/c |
m : multimethod? |
Retrieves the default signature of a multimethod.
(set-method m signature method) → multimethod? |
m : multimethod? |
signature : any/c |
method : procedure? |
Takes the multimethod m and transforms it into a new multimethod that maps the given signature to the method implementation method.
Any existing mapping for the given signature is replaced.
(remove-method m signature) → multimethod? |
m : multimethod? |
signature : any/c |
Takes the multimethod m and transforms it into a new multimethod that has no direct mapping for the given signature to any method implementation.
(prefer-method m signature-a signature-b) → multimethod? |
m : multimethod? |
signature-a : any/c |
signature-b : any/c |
Takes the multimethod m and transforms it into a new multimethod that prefers dispatching via signature-a to dispatching via signature-b.
(unprefer-method m signature-a signature-b) → multimethod? |
m : multimethod? |
signature-a : any/c |
signature-b : any/c |
Takes the multimethod m and transforms it into a new multimethod that does not prefer dispatching via signature-a to dispatching via signature-b.
(struct (exn:fail:multimethod exn:fail) (multimethod signature)) |
multimethod : multimethod? |
signature : any/c |
An exception raised to signal a problems with the given multimethod, specifically concerning the given signature.
(find-method m signature) → procedure? |
m : multimethod? |
signature : any/c |
Retrieves the method implementation for multimethod m that matches the given signature best.
The method lookup works as follows:
The current hierarchy used by the multimethod is retrieved using the getter procedure passed to make-multimethod. If this value changed since the last time the multimethod was called, the new value is stored and the dispatching cache of the multimethod is cleared, ie. it is reset to the same state in which it was created.
The dispatching cache is consulted to find a method for the signature. If one is found, the search terminates.
The result of subsequent search steps is later stored in the cache.
The method table is consulted to find a method directly associated with the signature. If one is found, the search terminates here.
A list of possible method implementations is built:
All ancestors of the signature registered in the hierarchy used by the multimethod are retrieved. Then any methods associated to those ancestor signatures are fetched from the method table.
If the signature satisfies class?, interface? or dict?, the whole method table is scanned for mappings from candidate signatures that are ancestors of the reference signature as determined by derived?.
All method implementations found in this way are sorted by their signatures. A signature sorts before another if one of the following conditions is met:
The first signature is derived? from the second one.
The first signature is registered as preferred to the second one.
There is a registered preference and the first signature is derived? from the preferred signature while the less preferred signature is derived? from the second signature.
If the resulting sorted list has exactly one entry, or if the sorting order between the first and second entries in the list is strict, the first entry of the list is used as the method implementation to call and the search terminates here.
If the list has multiple entries but the first two are not strictly sorted, an error is raised indicating ambiguous methods. You can resolve this situation by defining more specific methods or by registering preferences.
If no method implementation was found so far, the search is retried from using not the original signature, but the multimethod’s default signature.
If all steps up to now have failed to find a method. An error is raised indicating a lack of applicable method implementations.