undef()
import { undef } from "@fed1/jsos";
The JSFunction
undef removes a primary or qualified method from a multimethod.
The functions def, around, before,
after and undef
allow you to add and remove regular and
qualified methods to and from a multimethod.
As such, they are the main way that enables you to write plugins for existing code which can be added and removed while the software is running.
Usage
undef(multimethod, method)
Parameters
# | Parameter | Required? | Type | Default |
---|---|---|---|---|
1 | multimethod | yes | TGeneric | |
2 | method | yes | JSFunction |
1. multimethod
The multimethod from which the method shall be remove.
2. method
The method to be removed from the multimethod.
Return value
A JSBoolean
that is true
if the method was removed, false
otherwise.
Examples
In order to be able to remove a method from a multimethod, you need to have access to the original function of said method. There are multiple ways how you can add a function as a method and simultaneously keep a reference to it.
The first one is to utilize the return value of undef
:
import { def, undef, Generic } from "@fed1/jsos";
const multi = Generic.multi();
const myMethod = def(multi, [], () => {});
undef(multi, myMethod);
Another option is to use a named function:
def(multi, [], function myMethod() {});
undef(multi, myMethod);
You might be wondering why keeping a reference to the function is necessary in the first place. The reason for that is that multimethods can have multiple methods registered for the same signature.
So if we would have a hypothetical
getMethodFor(multi, signature)
function, you might get back a different function than the one you originally registered.
The undef
function can also remove qualified methods added with around
,
before
or after
:
const myAroundFn = around(multi, [], (fn, ...args) => fn(...args));
const myBeforeFn = before(multi, [], () => {});
const myAfterFn = after(multi, [], () => {});
undef(multi, myAroundFn);
undef(multi, myBeforeFn);
undef(multi, myAfterFn);
Version History
Feature | Since version |
---|---|
undef() | 1.0 |