I thought you may find it interesting that while back I wrote library to full 
fill my desire for clojure like protocols & wrote a blog post  
about it:

http://jeditoolkit.com/2012/03/21/protocol-based-polymorphism.html#post
https://github.com/Gozala/protocol

In a process of using it I discovered that it was not very javascripty & did 
another take on this in form of different library
that let's you create functions and provide it's polymorphic implementations:
https://github.com/Gozala/method

I & my team members found this really useful in real project that let us 
separate contracts between components that would
share some interface but are not necessarily related. Initially library was 
generated unique names for each function that you
create, although I hope to use private symbols once they're available:

var each = method()

In nutshell it would do something along this lines

const method = () => {
  const id = Math.random().toString(32).substr(2)
  const f = (self, …args) => self[id](self, …args)
  f.toString = () => id
  return f
}


Although dispatch is little more complicated to incorporate host objects and 
default implementations. Library also does not
mutate bulit-ins instead keeps internal hashes for them. Anyhow this enables 
defining implementations for this function
per type:

MyIterable.prototype[each] = (myIterable, f) => ….

Unfortunately later I had to move towards `method("your-own-unique-name")` 
approach (although old one is still there), because users on nodejs would 
constantly run into issues, because of nom's nutty habit of duplicating 
dependencies. That caused same library copy to have `each` functions with 
different identifiers :( I'm afraid we'll face same issues with private symbols 
as well.


A while ago I also proposed TC39 to consider a small change to private symbols 
such that, created symbols would actually be
invokable:

import Symbol from "@symbol";
const each = new Symbol("each", true);

each(iterator, x => x + 1)
Where above line would desugar to:

iterator[each](iterator, x => x + 1)




Regards
--
Irakli Gozalishvili
Web: http://www.jeditoolkit.com/


On Wednesday, 2013-10-23 at 13:02 , Russell Leggett wrote:

> On Wed, Oct 23, 2013 at 2:57 PM, Benjamin (Inglor) Gruenbaum 
> <ing...@gmail.com (mailto:ing...@gmail.com)> wrote:
> > Yes, this looks solid and definitely like something I'll use. I'll try to 
> > go through use cases and find problems during the weekend.
> >  
> > What do you think would be the fastest way to get a prototype something 
> > working to play with?  
> >  
>  
> Well the library itself could be implemented now, but without "::" you 
> wouldn't get the nice syntax. I've been looking into Traceur a bit to see how 
> hard it would be to add "::" - its some fairly simple syntax sugar.  
>  
> - Russ  
> _______________________________________________
> es-discuss mailing list
> es-discuss@mozilla.org (mailto:es-discuss@mozilla.org)
> https://mail.mozilla.org/listinfo/es-discuss
>  
>  


_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to