# class getter shorthand?

**URL:** <https://es.discourse.group/t/class-getter-shorthand/931>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [August 16, 2021, 8:34pm UTC](https://es.discourse.group/t/class-getter-shorthand/931 "2021-08-16T20:34:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NullVoxPopuli](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nullvoxpopuli/32/388_2.png) [@NullVoxPopuli](https://es.discourse.group/u/NullVoxPopuli)\
**Post date:** [August 16, 2021, 8:34pm UTC](https://es.discourse.group/t/class-getter-shorthand/931/1 "2021-08-16T20:34:50Z")

</div>

Has this been discussed before?

I'm imagining these two classes are behaviorally the exact same:

```javascript
class Renderer extends Component {
  @cached
  get result() {
    return compileHBS(this.args.input;
  }
  
  get component () {
    return this.result.component;
  }
  
  get error() {
    return this.result.error;
  }
  
  get name() {
    return this.result.name;
  }
}

```

and

```javascript
class Renderer extends Component {
  // syntax tbd, prior art: Svelte 
  // though they start a line with $: to have this meaning
  @cached result := compileHBS(this.args.input);
  
  component := this.result.component;
  error := this.result.error;
  name := this.result.name;
}

```

The main benefit is primarily for what-would-be-one-line getters, which provides a way to define aliases for other data in way that doesn't get burned into the class instance, like a normal property would

```javascript
class Renderer extends Component {
  // set once, never references this.result ever again
  component = this.result.component;
}

```

using getters allows us to reference `component` on `this.result` every time the getter is accessed.. it would be great to have a shorthand for this :)

thoughts?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [August 16, 2021, 8:48pm UTC](https://es.discourse.group/t/class-getter-shorthand/931/2 "2021-08-16T20:48:51Z")

</div>

There's this very recent discussion: [Shorthand For Inline Functions](https://es.discourse.group/t/shorthand-for-inline-functions/926/14).

The [decorators proposal](https://github.com/tc39/proposal-decorators) also includes an `accessor` modifier for fields which will automatically define getter and setter methods for the field, moving the value to a private backing field.

```javascript
class Example {
  accessor myBool = false;
}

```

This would include both the getter and setter, though. The [grouped accessors proposal](https://github.com/tc39/proposal-grouped-and-auto-accessors) would provide syntax for specifying only one.

```javascript
class Example {
  accessor myBool { get; } = false;
}

```

---

<div class="post-metadata">

**Author:** ![NullVoxPopuli](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nullvoxpopuli/32/388_2.png) [@NullVoxPopuli](https://es.discourse.group/u/NullVoxPopuli)\
**Post date:** [August 16, 2021, 9:00pm UTC](https://es.discourse.group/t/class-getter-shorthand/931/3 "2021-08-16T21:00:44Z")

</div>

> [@senocular](#):
>
> There's this very recent discussion: [Shorthand For Inline Functions](https://es.discourse.group/t/shorthand-for-inline-functions/926/14).

So this would propose:

```js
class Renderer extends Component {
  @cached get result() compileHBS(this.args.input);

  get component() this.result.component;
  get error() this.result.error;
  get name() this.result.name;
}

```

seems ok -- it's very clear that everything is still a getter and follows a similar pattern that inline ifs use.  
it's only 3 more characters than `:=` 🙃

> [@senocular](#):
>
> The [decorators proposal](https://github.com/tc39/proposal-decorators) also includes an `accessor` modifier for fields which will automatically define getter and setter methods for the field, moving the value to a private backing field.

combining decorators with accessors seems like it would be _more_ code than today's vanilla getters. While the next iteration of the decorator proposal would change subtle things about my `result` getter, I don't want to have to use a getter for what-would-be-a-one-line-getter. 🤔
