fmap-archive / typeclasses.js

Proof-of-concept implementation of typeclasses in Javascript.
BSD 2-Clause "Simplified" License
2 stars 0 forks source link

Change interface? #4

Open fmap opened 10 years ago

fmap commented 10 years ago

Proposal: rather than retrieve generics based on values, we could have the interface provide functions for you, whose values change based on arguments.. Typeclasses are instances of a class Typeclass, which provides an extend method, whereby new instances can be appended. When a typeclass' generic receives a value, it looks for an implementation against the defaults and its extensions, , baulking if it can't find one compatible..

With this, out concat example could be rewritten:

function mconcat (xs) {
  return Foldable.foldr(xs, Monoid.plus, Monoid.zero);
};
fmap commented 10 years ago

While modifying object prototypes would give us a pretty interface, we'd have to worry about conflicts between libraries. @yorickvp has proposed using ES6 WeakMaps instead:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap. https://github.com/Benvie/WeakMap.

fmap commented 10 years ago

So I figure we'd be providing each typeclass an extensible map, which given some constructor, returns the appropriate function implementations?

@LeopoldTal How's thinking/coding progressing here?