Multi-type protocol-based polymorphism
npm install @zkat/protocolsprotocols is a JavaScript library for
defining and implementing typeclass-like protocols that support dispatch across
multiple arguments. See also: Clojure
protocols but on any number of types
across all arguments of the individual functions.
protocols is built on top of genfun, a fast,
prototype-based multimethod library, but this is mostly all behind the scenes.
On top of providing a nice, clear interface for defining interfaces, this
module makes the best effort to verify both protocols and implementations and
tries to provide clear, useful errors for common mistakes.
$ npm install @zkat/protocols
* API
* protocol()
* implementation
Defines a new protocol on across arguments of types defined by , which
will expect implementations for the functions specified in .
If is missing, it will be treated the same as if it were an empty
array.
The types in must map, by string name, to the type names specified in, or be an empty array if is omitted. The types in
will then be used to map between method implementations for the individual
functions, and the provided types in the impl.
##### Example
``javascript`
const Eq = protocol(['a', 'b'], {
eq: ['a', 'b']
})
Adds a new implementation to the given proto across .
must be an object with functions matching the protocol's
API. The types in will be used for defining specific methods using
the function as the body.
Protocol implementations must include either , , or both:
* If only is present, implementations will be defined the same as
"traditional" methods -- that is, the definitions in
will add function properties directly to .
* If only is present, the protocol will keep all protocol functions as
"static" methods on the protocol itself.
* If both are specified, protocol implementations will add methods to the , and define multimethods using .
If a protocol is derivable -- that is, all its functions have default impls,
then the object can be omitted entirely, and the protocol
will be automatically derived for the given
##### Example
`javascript
import protocol from '@zkat/protocols'
// Singly-dispatched protocols
const Show = protocol({
show: []
})
class Foo {}
Show(Foo, {
show () { return [object Foo(${this.name})] }
})
var f = new Foo()
f.name = 'alex'
f.show() === '[object Foo(alex)]'
`
`javascript
import protocol from '@zkat/protocols'
// Multi-dispatched protocols
const Comparable = protocol(['target'], {
compare: ['target'],
})
class Foo {}
class Bar {}
class Baz {}
Comparable(Foo, [Bar], {
compare (bar) { return 'bars are ok' }
})
Comparable(Foo, [Baz], {
compare (baz) { return 'but bazzes are better' }
})
const foo = new Foo()
const bar = new Bar()
const baz = new Baz()
foo.compare(bar) // 'bars are ok'
foo.compare(baz) // 'but bazzes are better'
``