Arrow functions as methods

Irakli Gozalishvili rfobic at gmail.com
Wed Jul 11 13:22:23 PDT 2012


> 
> We've been around that block and will go again. If we want convenient 
> unbound-this function syntax, -> stands ready. I proposed it along with 
> => originally but it was cut by TC39 during our March meeting in order 
> to achieve consensus on =>.
> 

Yeah I'm aware of that, but problem as I see is that new fat arrow functions can not fully replace existing functions. In combination with thin arrows they would but since they have being cut ended up with a dependency on an old style function in order to wrap arrows to make them usable as methods. I consider it's a failure.
 
> 
> 
> > object.foo(bar)
> > 
> > If `object.foo` is an arrow function it has no way of knowing weather 
> > it was invoked as a function or a method. Neither it get's reference 
> > to an `object`. This makes composition between functional & OO styles 
> > components hard.
> > 
> 
> 
> No, I don't think so. The ability of a function to ignore any parameter, 
> including |this|, may frustrate composition, but the contract of an API 
> includes mandatory parameterization of any funargs.
> 
> People deal with this pretty well in practice, usually by writing 
> small-world codebases and wrapping anything unknown that needs 
> contract-enforcement along these lines.
> 
> 


Well but as pointed out above you need an old style functions to make arrows usable as methods, I think such dependency is very unfortunate.
> 
> > Passing owner `object` of the arrow method as first argument solves 
> > this problem, by making
> > 
> > object.foo(bar)
> > 
> > just a sugar for
> > 
> > var foo = object.foo;
> > foo(object, bar);
> > 
> 
> 
> This completely breaks functional abstraction, of course.
> 
> f(a)
> 
> and
> 
> o.m(a)
> 
> in JS should never pass more than one actual parameter to the callee, 
> however it was declared or expressed.
> 
> 


Unfortunately I still do not understand why is that a problem, specially since Claus's wrapper can be used to achieve that. It's just I think it makes it a much better default.

> 
> /be
> 
> > 
> > 
> > Regards
> > --
> > Irakli Gozalishvili
> > Web: http://www.jeditoolkit.com/
> > 
> > On Wednesday, 2012-07-11 at 10:23 , Irakli Gozalishvili wrote:
> > 
> > > It is true that on could wrap arrow function into vintage one in 
> > > order to reuse it as a method, but that is a poor mans workaround. As 
> > > of motivation, all I really want is to have a synergy of different 
> > > paradigms that are in the language.
> > > 
> > > What I really mean by that is, if one writes in a functional style:
> > > 
> > > var map = (list, f) =>
> > > reduce(list, (result, value) =>
> > > result.concat([ f(value) ]), [])
> > > 
> > > That should be reusable in OO code without any changes or wrappers:
> > > 
> > > List.prototype.map = map
> > > List().map((x) => x + 1)
> > > 
> > > Also this complements another idea that I have posted a while ago:
> > > https://mail.mozilla.org/pipermail/es-discuss/2012-June/023657.html
> > > 
> > > Where I proposed to make private names functions:
> > > 
> > > var method = Name()
> > > method(object, arg) // is just a sugar for
> > > object[method](arg)
> > > 
> > > This will enable people to write code in OO or functional style and 
> > > make it usable in other style:
> > > 
> > > var map = Name();
> > > List.prototype[map] = (list, f) =>
> > > reduce(list, (result, value) => result.concat([ f(value) ]), [])
> > > 
> > > Which one will be able to us in a functional style
> > > 
> > > map(list, f)
> > > 
> > > or in OO
> > > 
> > > list[map](f)
> > > 
> > > Also even if suggested changes to private names won't take of, it's 
> > > still can be implemented as a library:
> > > https://github.com/Gozala/method
> > > 
> > > There for a way to author functions that also can be used as methods 
> > > is relevant. Thin arrows would probably also solve this problem.
> > > 
> > > Regards
> > > --
> > > Irakli Gozalishvili
> > > Web: http://www.jeditoolkit.com/
> > > 
> > > On Wednesday, 2012-07-11 at 09:39 , Claus Reinke wrote:
> > > 
> > > > > function map(f) {
> > > > > return this.reduce((result, value) => result.concat([ f(value) ]), [])
> > > > > }
> > > > > ..
> > > > > var map = (list, f) =>
> > > > > list.reduce((result, value) => result.concat([ f(value) ]), [])
> > > > > 
> > > > 
> > > > 
> > > > Not sure I got your motivation, but would this help?
> > > > 
> > > > function fn(f) { return f(this) } // provide 'this' as explicit arg
> > > > 
> > > > let map = it => f =>
> > > > it.list.reduce((result, value) => result.concat([ f(value) ]), [])
> > > > 
> > > > let obj = { map: fn(map), list: [1,2,3] }; // wrap map as method
> > > > 
> > > > obj.map(x => x+1);
> > > > map( {list:[1,2,3]} )(x => x+1);
> > > > 
> > > > Claus
> > 
> > _______________________________________________
> > es-discuss mailing list
> > es-discuss at mozilla.org (mailto:es-discuss at mozilla.org)
> > https://mail.mozilla.org/listinfo/es-discuss
> > 
> 
> 
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120711/9439b40e/attachment-0001.html>


More information about the es-discuss mailing list