[Idea] Bind operator as some sort of property acessor
# Logan Smyth (8 years ago)
Unless I'm misunderstanding what you are looking for, I believe the current
proposal accounts for this as ::arr.push
with no expression before the
::
.
Logan
Unless I'm misunderstanding what you are looking for, I believe the current proposal accounts for this as `::arr.push` with no expression before the `::`. Logan On Tue, Mar 7, 2017 at 4:33 PM, Augusto Moura <augusto.borgesm at gmail.com> wrote: > Before I open any suggestion for my ideia in the [bind-operator](// > github.com/tc39/proposal-bind-operator) proposal, I like to know you > guys' opinion about, and if even it is in the scope of the current proposal. > > There are moments when we want to export instances methods binded with > their objects, like `num.toFixed.bind (num)`, most of the time to be used > as high order functions. With the current proposal the best we can get is > `num::num.toFixed`, what seems a bit weird and counterintuitive. > > For example, let's say that for an unknow reason we want to copy a array > to another using the `push` method. There are a few ways: > ``` .javascript > const unary = fun => a => fun(a); > const arr = []; > > > // With arrow functions > [1, 2, 3, 4].map(n => arr.push(n)); > > > // With binding > [1, 2, 3].map(unary(arr.push.bind(arr))); > ``` > > And again, the best way of doing this with the current proposal is > something like: > ``` .javascript > [1, 2, 3].map(unary(arr::arr.push)); > ``` > > My idea, similar to double colon operator in Java 8, is that the > expression `foo::bar` translates to: access `bar` in `foo`, if it is a > function, returns the function binded to foo. > > So: > ``` .javascript > // The two expressions below produce the same function > foo::bar; > foo.bar.bind(foo); > > // So in our example instead of using something like > [1, 2, 3].map(arr::arr.push); > // we can use > [1, 2, 3].map(arr::push); > ``` > > And yeah, it just decreases one word. However imagine the symbol as > acessor instead of a operator, leaves the things really less confusing in > my opinion. > > It can be useful for React applications too: > ```.javascript > class Button extends React.Component { > alertText() { > alert(this.props.text); > } > > render() { > // Instead of `this::this.alertText` > return <button onClick={this::alertText} />; > } > } > ``` > > Imo, my idea has a different motivation e use case than the current > proposal and because of that, needs to be splitted with the original. We > can discuss other symbols than the `::`, but I personally think that it > fits perfectly and make some neat code when composing high order functions. > > -- > Augusto B. Moura > > _______________________________________________ > es-discuss mailing list > 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/20170307/da57381e/attachment-0001.html>
# Augusto Moura (8 years ago)
Yeah, you are right. The docs seems a bit confusing and I didn't notice it. Thanks for the attention
Yeah, you are right. The docs seems a bit confusing and I didn't notice it. Thanks for the attention Em ter, 7 de mar de 2017 às 21:43, Logan Smyth <loganfsmyth at gmail.com> escreveu: Unless I'm misunderstanding what you are looking for, I believe the current proposal accounts for this as `::arr.push` with no expression before the `::`. Logan On Tue, Mar 7, 2017 at 4:33 PM, Augusto Moura <augusto.borgesm at gmail.com> wrote: Before I open any suggestion for my ideia in the [bind-operator](// github.com/tc39/proposal-bind-operator) proposal, I like to know you guys' opinion about, and if even it is in the scope of the current proposal. There are moments when we want to export instances methods binded with their objects, like `num.toFixed.bind (num)`, most of the time to be used as high order functions. With the current proposal the best we can get is `num::num.toFixed`, what seems a bit weird and counterintuitive. For example, let's say that for an unknow reason we want to copy a array to another using the `push` method. There are a few ways: ``` .javascript const unary = fun => a => fun(a); const arr = []; // With arrow functions [1, 2, 3, 4].map(n => arr.push(n)); // With binding [1, 2, 3].map(unary(arr.push.bind(arr))); ``` And again, the best way of doing this with the current proposal is something like: ``` .javascript [1, 2, 3].map(unary(arr::arr.push)); ``` My idea, similar to double colon operator in Java 8, is that the expression `foo::bar` translates to: access `bar` in `foo`, if it is a function, returns the function binded to foo. So: ``` .javascript // The two expressions below produce the same function foo::bar; foo.bar.bind(foo); // So in our example instead of using something like [1, 2, 3].map(arr::arr.push); // we can use [1, 2, 3].map(arr::push); ``` And yeah, it just decreases one word. However imagine the symbol as acessor instead of a operator, leaves the things really less confusing in my opinion. It can be useful for React applications too: ```.javascript class Button extends React.Component { alertText() { alert(this.props.text); } render() { // Instead of `this::this.alertText` return <button onClick={this::alertText} />; } } ``` Imo, my idea has a different motivation e use case than the current proposal and because of that, needs to be splitted with the original. We can discuss other symbols than the `::`, but I personally think that it fits perfectly and make some neat code when composing high order functions. -- Augusto B. Moura _______________________________________________ es-discuss mailing list es-discuss at mozilla.org https://mail.mozilla.org/listinfo/es-discuss -- Augusto B. Moura -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170308/5f612e9a/attachment.html>
# Leo Balter (8 years ago)
The array map was a bad choice for the examples and got me distracted. Anyway, the example for the operator (arr::push) is something that I agree with you and I believe I suggested this at the proposal more than an year ago. If im not wrong, there's a thread like issue discussing this.
::arr.push does not feel natural and much more if I have a property chain: ::foo.bar.baz. It looks ambiguous.
The array map was a bad choice for the examples and got me distracted. Anyway, the example for the operator (arr::push) is something that I agree with you and I believe I suggested this at the proposal more than an year ago. If im not wrong, there's a thread like issue discussing this. ::arr.push does not feel natural and much more if I have a property chain: ::foo.bar.baz. It looks ambiguous. On Tue, Mar 7, 2017 at 8:10 PM Augusto Moura <augusto.borgesm at gmail.com> wrote: > Yeah, you are right. The docs seems a bit confusing and I didn't notice > it. Thanks for the attention > > > Em ter, 7 de mar de 2017 às 21:43, Logan Smyth <loganfsmyth at gmail.com> > escreveu: > > Unless I'm misunderstanding what you are looking for, I believe the > current proposal accounts for this as `::arr.push` with no expression > before the `::`. > > Logan > > On Tue, Mar 7, 2017 at 4:33 PM, Augusto Moura <augusto.borgesm at gmail.com> > wrote: > > Before I open any suggestion for my ideia in the [bind-operator](// > github.com/tc39/proposal-bind-operator) proposal, I like to know you > guys' opinion about, and if even it is in the scope of the current proposal. > > There are moments when we want to export instances methods binded with > their objects, like `num.toFixed.bind (num)`, most of the time to be used > as high order functions. With the current proposal the best we can get is > `num::num.toFixed`, what seems a bit weird and counterintuitive. > > For example, let's say that for an unknow reason we want to copy a array > to another using the `push` method. There are a few ways: > ``` .javascript > const unary = fun => a => fun(a); > const arr = []; > > > // With arrow functions > [1, 2, 3, 4].map(n => arr.push(n)); > > > // With binding > [1, 2, 3].map(unary(arr.push.bind(arr))); > ``` > > And again, the best way of doing this with the current proposal is > something like: > ``` .javascript > [1, 2, 3].map(unary(arr::arr.push)); > ``` > > My idea, similar to double colon operator in Java 8, is that the > expression `foo::bar` translates to: access `bar` in `foo`, if it is a > function, returns the function binded to foo. > > So: > ``` .javascript > // The two expressions below produce the same function > foo::bar; > foo.bar.bind(foo); > > // So in our example instead of using something like > [1, 2, 3].map(arr::arr.push); > // we can use > [1, 2, 3].map(arr::push); > ``` > > And yeah, it just decreases one word. However imagine the symbol as > acessor instead of a operator, leaves the things really less confusing in > my opinion. > > It can be useful for React applications too: > ```.javascript > class Button extends React.Component { > alertText() { > alert(this.props.text); > } > > render() { > // Instead of `this::this.alertText` > return <button onClick={this::alertText} />; > } > } > ``` > > Imo, my idea has a different motivation e use case than the current > proposal and because of that, needs to be splitted with the original. We > can discuss other symbols than the `::`, but I personally think that it > fits perfectly and make some neat code when composing high order functions. > > -- > Augusto B. Moura > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > > -- > Augusto B. Moura > _______________________________________________ > es-discuss mailing list > 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/20170310/6beefcd1/attachment-0001.html>
Before I open any suggestion for my ideia in the [bind-operator](// github.com/tc39/proposal-bind-operator) proposal, I like to know you guys' opinion about, and if even it is in the scope of the current proposal.
There are moments when we want to export instances methods binded with their objects, like
num.toFixed.bind (num)
, most of the time to be used as high order functions. With the current proposal the best we can get isnum::num.toFixed
, what seems a bit weird and counterintuitive.For example, let's say that for an unknow reason we want to copy a array to another using the
push
method. There are a few ways:And again, the best way of doing this with the current proposal is something like:
My idea, similar to double colon operator in Java 8, is that the expression
foo::bar
translates to: accessbar
infoo
, if it is a function, returns the function binded to foo.So:
And yeah, it just decreases one word. However imagine the symbol as acessor instead of a operator, leaves the things really less confusing in my opinion.
It can be useful for React applications too:
Imo, my idea has a different motivation e use case than the current proposal and because of that, needs to be splitted with the original. We can discuss other symbols than the
::
, but I personally think that it fits perfectly and make some neat code when composing high order functions.