Skip to content
This repository has been archived by the owner on Feb 19, 2018. It is now read-only.

CS2 Discussion: Features: Getters / Setters #82

Closed
CliffS opened this issue May 5, 2017 · 5 comments
Closed

CS2 Discussion: Features: Getters / Setters #82

CliffS opened this issue May 5, 2017 · 5 comments

Comments

@CliffS
Copy link

CliffS commented May 5, 2017

I have been following the discussions on getters/setters and I totally understand and concur with the reasoning behind not allowing the standard ES6 syntax in CS2.

However, the solution given in the draft manual is, IMO, both unclear and against the general philosophy of CS which is to make things less verbose and easier to use. I am constantly pushing CS as a more concise way of writing JS and having to write the following is simply counter-intuitive.

class  screen

  width: 1200
  ratio: 16/9

  Object.defineProperty @, 'height',
    get: ->
      this.width / this.ratio
    set: (val) ->
      this.width = val / this.ratio

What about introducing a new syntax? Something on the lines of:

class screen

  width: 1200
  ratio: 16/9

  |get| width: ->
      this.width / this.ratio
  |set| width: (val) ->
      this.width = val / this.ratio

Thoughts?

@GeoffreyBooth
Copy link
Collaborator

GeoffreyBooth commented May 5, 2017

Getters and setters weren't rejected because we couldn't come up with a workable syntax. They were rejected because it's a language feature that's discouraged. The note in the docs isn't to say "this is how you should do getters and setters in CoffeeScript," it's to say that we discourage them but if you insist you can use them using this verbose syntax.

@joeldrapper
Copy link

@GeoffreyBooth Why are they discouraged?

@GeoffreyBooth
Copy link
Collaborator

See #17

@joeldrapper
Copy link

joeldrapper commented May 6, 2017

Thanks @GeoffreyBooth, I’ll take a look at that thread.

I think they can be implemented quite easily with class methods if people want to use them. Something like this could work.

class BaseObject
  @get: (name, fun) ->
    Object.defineProperty @::, name,
      get: ->
        fun()

  @set: (name, fun) ->
    Object.defineProperty @::, name,
      set: (value) ->
        fun value

Edit: I found a much better way to do this.

@coffeescriptbot coffeescriptbot changed the title Getters / Setters CS2 Discussion: Features: Getters / Setters Feb 19, 2018
@coffeescriptbot
Copy link
Collaborator

Migrated to jashkenas/coffeescript#4975

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants