Generator getter method

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Generator getter method

Mohsen Azimi
Can we allow generator getter methods?

Here is an example use case:

```
let rectangle = {
  start: {x: 1, y: 5},
  width: 2,
  height: 3,
  
  *getCorners() {
    let {x: startX, y: startY} = this.start;
    yield this.start;
    yield {x: startX + this.width, y: startY};
    yield {x: startX + this.width, y: startY + this.height};
    yield {x: startX, y: startY + this.height}
  }
}

for (let corner of rectangle.getCorners()) {
  console.log(corner);
}
```

I'm proposing to allow  `get coreners* () { ...` (a generator getter method) so the for..of loop can be written like this:

```
for (let corner of rectangle.corners) {
  console.log(corner);
}
```

Which seems cleaner 

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Generator getter method

Kevin Smith
The existing option is:

let rect = {
  get corners() { return this._corners(); },
  *_corners() { .. },
};

which doesn't seem so bad.  Also, a generator getter would introduce a bit of divergence between getter and setter syntax.

On Tue, Nov 10, 2015 at 2:28 PM Mohsen Azimi <[hidden email]> wrote:
Can we allow generator getter methods?

Here is an example use case:

```
let rectangle = {
  start: {x: 1, y: 5},
  width: 2,
  height: 3,
  
  *getCorners() {
    let {x: startX, y: startY} = this.start;
    yield this.start;
    yield {x: startX + this.width, y: startY};
    yield {x: startX + this.width, y: startY + this.height};
    yield {x: startX, y: startY + this.height}
  }
}

for (let corner of rectangle.getCorners()) {
  console.log(corner);
}
```

I'm proposing to allow  `get coreners* () { ...` (a generator getter method) so the for..of loop can be written like this:

```
for (let corner of rectangle.corners) {
  console.log(corner);
}
```

Which seems cleaner 
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Generator getter method

Bergi
In reply to this post by Mohsen Azimi
Mohsen Azimi schrieb:

> I'm proposing to allow  `get corners* () { ...` (a generator getter
> method) so the for..of loop can be written like this:
>
> ```
> for (let corner of rectangle.corners) {
>    console.log(corner);
> }
> ```

I would avoid that. `rectangle.corners !== rectangle.corners` could be
quite confusing. Creating the generator explicitly is a better idea.
And as kevin said, you still can return generators from your getters
manually if you really want that.

Regards,
  Bergi
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Generator getter method

Rick Waldron
In reply to this post by Mohsen Azimi


On Tue, Nov 10, 2015 at 2:28 PM Mohsen Azimi <[hidden email]> wrote:

for (let corner of rectangle.getCorners()) {
  console.log(corner);
}
```

for (let corner of rectangle.corners) {
  console.log(corner);
}
```

Which seems cleaner 

Either could've been called "corners" and then the only difference is the invocation parens.


Rick

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss