new Trait(name, sym)
Create a new Trait.
Create a new Trait.
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
-
impl()
Implement this trait for a class as a 'method'.
-
Implement this trait for a class as a 'method'. See examples above
-
implDerived()
Implements a trait based on other traits
. -
Implements a trait based on other traits
-
implStatic()
Implement this trait for a value/as a 'static method'.
-
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().
-
implWild()
Arbitrary code implementation of this trait for types.
-
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.
-
Arbitrary code implementation of this trait for values. See examples above
-
invoke()
Invoke the implementation.
-
Invoke the implementation. See examples above.
-
lookupType()
Lookup the implementation of this trait for a specific type.
-
Lookup the implementation of this trait for a specific type. Pretty much the same as lookupValue, just skips the value lookup steps…
-
lookupValue(what) → {function|falsy-value}
Find the implementation of this trait for a specific 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 whatAny The thing to find an implementation for
Returns:
function | falsy-value -The function that was found or nothing. Takes the same parameters as
.invoke(what, ...args), so if you are not using invoke, you must specifywhattwice; once in thelookupValuecall, once in the invocation. - Implementations added with