Protocol library as alternative to refinements (Russell Leggett)

Benjamin (Inglor) Gruenbaum inglor at gmail.com
Wed Oct 23 11:57:49 PDT 2013


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?


On Tue, Oct 22, 2013 at 11:07 PM, Russell Leggett <russell.leggett at gmail.com
> wrote:

> On Tue, Oct 22, 2013 at 2:34 PM, Benjamin (Inglor) Gruenbaum <
> inglor at gmail.com> wrote:
>
>> On Tue, Oct 22, 2013 at 8:10 PM, Russell Leggett <
>> russell.leggett at gmail.com> wrote:
>>
>>> > Revised algorithm:
>> > 1. If receiver has protocol method symbol as a property, use that as
>> override.
>> > 2. Try to use protocol methods - start by checking receiver type
>> mapping, then check up type hierarchy for any matches, and finally if no
>> matches, use the default if defined.
>> > 3. Finally, if no matches and no default, check prototype for method of
>> same name.
>> > Does that sound better?
>>
>> Much :)
>>
>
> Actually, let me revise 3:
> 3. Finally, if no matches and no default, attempt to call a method of the
> same name (not affected by variable name or aliasing) on the receiver.
>
> Something like this:
>
>     const P = new Protocol("foo");
>
>     // naive, non-native implementation of P.methods.foo would be
> something like
>     function foo(...args){
>         if(P.symbols.foo in this){
>             //P.symbols holds a symbol for each method
>             return this[P.symbol.foo](...args);
>
>         }else if(P.contains(Object.getPrototypeOf(this))){
>             //contains and lookup might be backed by a weakmap or
> something,
>             //but it would also need to go up the type chain
>             let myFoo = P.lookup(Object.getPrototypeOf(this)).foo;
>             return this::myFoo(...args);
>
>         }else if(P.getDefaults().hasOwnProperty("foo")){
>             let defaultFoo = P.getDefaults().foo;
>             return this::defaultFoo(...args);
>
>         }else{
>             this.foo(...args);
>         }
>     }
>
> If this seems acceptable, I'll update the gist.
>
> - Russ
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131023/2b339062/attachment.html>


More information about the es-discuss mailing list