trait

In­tro­du­cing type classes (from haskell)/​traits (from rust) to javas­cript.

Source:

Classes

HybridWeakMap
Trait
TraitNotImplemented

Methods

(inner) supports(Typ, trait) → {Boolean}

Source:

Test if the given trait has been im­ple­men­ted for the given type

const as­sert = re­quire('assert');
const { Size, sup­ports } = re­quire('fer­rum');

class Bar {};
as­sert(!sup­ports(Bar, Size));

// can be cur­ried
const sup­portsS­ize = sup­ports(Size);

// After im­ple­ment­ing the trait
Size.impl(Bar, () => 42);
as­sert(sup­portsS­ize(Bar));
Parameters:
Name Type Description
Typ function

The type

trait Trait

The trait to check sup­port for

Returns:
Type
Boolean

(inner) valueSupports(Typ, trait) → {Boolean}

Source:

Test if the given trait has been im­ple­men­ted for the given value

const as­sert = re­quire('assert');
const { Size, val­ue­Sup­ports } = re­quire('fer­rum');

class Bar {};
as­sert(!val­ue­Sup­ports(new Bar(), Size));

// can be cur­ried
const val­ue­Sup­portsS­ize = val­ue­Sup­ports(Size);

// After im­ple­ment­ing the trait
Size.impl(Bar, () => 42);
as­sert(val­ue­Sup­portsS­ize(new Bar()));
Parameters:
Name Type Description
Typ function

The type

trait Trait

The trait to check sup­port for

Returns:
Type
Boolean