jussi.kalliokoski at gmail.com (2015-06-17T07:10:20.796Z)
It's probably a bit early for this, but I figured I'd put it out there (I
already proposed this as a tangent in the function bind syntax thread).
This syntax proposal is purely about convenience and subjective
expressiveness (like any feature addition to a Turing complete language).
As I've been building Trine, I noticed that the "`this` as data" pattern is
extremely powerful and expressive, however the code in the functions
doesn't convey the intent very clearly. For example:
```JS
function add (b) { return this + b; }
function * map (fn) { for ( let item of this ) { yield item::fn(); } }
```
vs.
```JS
function add (a, b) { return a + b; }
function * map (iterator, fn) { for ( let item of iterator ) { yield
item::fn(); } }
```
Also currently neither Flow or TypeScript support type annotating this.
There's discussion [1] [2] in both the projects for allowing `this` to be
specified as a parameter to allow annotating it, e.g.
```JS
function add (this : number, b : number) : number { return this + b; }
```
This leads to my current proposal, i.e. being able to make the first
parameter of the function an alias for `this` by using a special prefix
(&). This would not only allow aliasing `this`, but also destructuring and
default values (as well as type annotation in language extensions).
The different forms and their desugarings:
```JS
function add (&a, b) {
return a + b;
}
// would desugar to
function add (b) {
var a = this;
return a + b;
}
function multiplyTuple (&[a, b], multiplier) {
return [a * multiplier, b * multiplier];
}
// would desugar to
function multiplyTuple (multiplier) {
var [a, b] = this;
return [a * multiplier, b * multiplier];
}
function multiplyPoints (&{ x1: x, y1: y }, { x2: x, y2: y }) {
return { x: x1 * x2, y: y1 * y2 };
}
// would desugar to
function multiplyPoints (_p2) {
var { x1: x, y1: y } = this;
var { x2: x, y2: y } = _p2;
return { x: x1 * x2, y: y1 * y2 };
}
// allow passing the element for mocking in tests
function isQsaSupported (&dummyElement = document) {
return typeof dummyElement.querySelectorAll !== "undefined";
}
```
This proposal would also be consistent with the type annotation proposals
for `this` mentioned earlier.
WDYT?
[1] https://github.com/facebook/flow/issues/452
[2] https://github.com/Microsoft/TypeScript/issues/1985
It's probably a bit early for this, but I figured I'd put it out there (I already proposed this as a tangent in the function bind syntax thread). This syntax proposal is purely about convenience and subjective expressiveness (like any feature addition to a Turing complete language). As I've been building Trine, I noticed that the "`this` as data" pattern is extremely powerful and expressive, however the code in the functions doesn't convey the intent very clearly. For example: function add (b) { return this + b; } function * map (fn) { for ( let item of this ) { yield item::fn(); } } vs. function add (a, b) { return a + b; } function * map (iterator, fn) { for ( let item of iterator ) { yield item::fn(); } } Also currently neither Flow or TypeScript support type annotating this. There's discussion [1] [2] in both the projects for allowing `this` to be specified as a parameter to allow annotating it, e.g. function add (this : number, b : number) : number { return this + b; } This leads to my current proposal, i.e. being able to make the first parameter of the function an alias for `this` by using a special prefix (&). This would not only allow aliasing `this`, but also destructuring and default values (as well as type annotation in language extensions). The different forms and their desugarings: function add (&a, b) { return a + b; } // would desugar to function add (b) { var a = this; return a + b; } function multiplyTuple (&[a, b], multiplier) { return [a * multiplier, b * multiplier]; } // would desugar to function multiplyTuple (multiplier) { var [a, b] = this; return [a * multiplier, b * multiplier]; } function multiplyPoints (&{ x1: x, y1: y }, { x2: x, y2: y }) { return { x: x1 * x2, y: y1 * y2 }; } // would desugar to function multiplyPoints (_p2) { var { x1: x, y1: y } = this; var { x2: x, y2: y } = _p2; return { x: x1 * x2, y: y1 * y2 }; } // allow passing the element for mocking in tests function isQsaSupported (&dummyElement = document) { return typeof dummyElement.querySelectorAll !== "undefined"; } This proposal would also be consistent with the type annotation proposals for `this` mentioned earlier. WDYT? [1] https://github.com/facebook/flow/issues/452 [2] https://github.com/Microsoft/TypeScript/issues/1985 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150617/720e5fe0/attachment-0001.html>