Syntax sugar for partial application
liorean at gmail.com
Sat Apr 11 01:54:39 UTC 2015
On 9 April 2015 at 16:11, Jussi Kalliokoski <jussi.kalliokoski at gmail.com> wrote:
> On Thu, Apr 9, 2015 at 4:04 PM, liorean <liorean at gmail.com> wrote:
>> Do we really need it?
>> Your «foo(1, ?, 2);» is equivalent to «a=>foo(1,a,2)».
>> Your «foo(?, 1, ???);» is equivalent to «(a,...b)=>foo(a,1,...b)».
>> Your «foo(1, ???, 2);» is equivalent to «(...a)=>foo(...[1,...a,2])».
> Not exactly. Using the placeholder syntax, `this` remains context dependent,
> whereas with your examples you get `null` as `this`.
No, «this» is lexically bound to be that of the enclosing lexical
scope in arrow functions, so it would be whatever that is. But that
doesn't really matter as the function call to «foo» doesn't use the
«this» of the arrow function.
Now, if we were to say your «foo» were actually «foo.bar», and you did
the same replacement in the arrow function, the «this» value of the
«bar» call would be «foo», so that's pretty much what is wanted as
well. The case where this breaks is if you were to replace only the
«bar» method with the arrow function, in which case it would use the
lexical «this» instead of «foo», but that's obviously not the right
transformation to use.
> This might not seem like such a big deal until you consider it in
> combination with the proposed bind syntax .
> Also in your examples, redefining `foo` will lead to different results. The
> placeholder syntax has a lot more room for optimization in the JIT compiler
> (the partially applied result is guaranteed to have no side effects for
> example, so the compiler can create a version of the original function where
> it can inline the specified arguments; less moving parts, easier to
Yeah, it's susceptible to that problem, yes. Do you want me to fix
that for you if you really want it?
Your «foo(1, ?, 2);» is equivalent to «((f,a)=>f(1,a,2))(foo)».
Your «foo(?, 1, ???);» is equivalent to «((f,a,...b)=>f(a,1,...b))(foo)».
Your «foo(1, ???, 2);» is equivalent to «((f,...a)=>f(...[1,...a,2]))(foo)».
I guess I didn't think of these cases though, because I only use
explicit arguments to my functions these days, I never use the «this»
keyword. If I want a function to operate on an object, I pass that
object into the function.
I also try to not reuse my variables unless they are part of an
iteration, in which case they are always local variables that are only
handled in the iteration process itself. But that's a side issue, as
it's about my code rather than precepts of the language.
David "liorean" Andersson
More information about the es-discuss