Constructor
new Trait(name, sym)
Properties:
| Name | Type | Description |
|---|---|---|
name |
String | undefined | The name of the trait |
sym |
Symbol | The symbol for lookup inside third party classes |
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string | The name of the trait |
sym |
Symbol | null | Symbol associated with the trait; this symbol
will be available under |
Methods
lookupValue(what) → {function|falsy-value}
Find the implementation of this trait for a specific value.
This is used by .invoke(), .supports() and .valueSupports.
It uses the following precedence by default:
- Implementations added with
implStatic - Implementations using the symbol in a method of a prototype
- Implementations added with
impl - Implementations added with
implDerivedin the order they where added - Implementations added with
implWildin the order… - Implementations added with
implWildStaticin the order…
This function can be used directly in order to avoid a double lookiup of the implementation:
const impl = MyTrait.lookupValue(what);
if (impl) {
impl(what, ...);
} else {
...
}
Parameters:
| Name | Type | Description |
|---|---|---|
what |
Any | The thing to find an implementation for |
Returns:
The function that was found or nothing.
Takes the same parameters as .invoke(what, ...args), so if you are not
using invoke, you must specify what twice; once in the lookupValue call, once
in the invocation.
- Type
- function | falsy-value
lookupType()
Lookup the implementation of this trait for a specific type. Pretty much the same as lookupValue, just skips the value lookup steps…
invoke()
Invoke the implementation. See examples above.
impl()
Implement this trait for a class as a 'method'. See examples above
implStatic()
Implement this trait for a value/as a 'static method'. See examples above Prefer impl() when possible since implementations using this function will not show up in supports()/this.typeHasImpl().
implDerived()
Implements a trait based on other traits
implWild()
Arbitrary code implementation of this trait for types. See examples above Prefer implWild() when possible since implementations using this function will not show up in supports()/this.typeHasImpl().
implWildStatic()
Arbitrary code implementation of this trait for values. See examples above