Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS2 Discussion: Features: Getters / Setters #4975

Closed
coffeescriptbot opened this issue Feb 19, 2018 · 4 comments
Closed

CS2 Discussion: Features: Getters / Setters #4975

coffeescriptbot opened this issue Feb 19, 2018 · 4 comments

Comments

@coffeescriptbot
Copy link
Collaborator

From @CliffS on 2017-05-05 09:50

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?

@coffeescriptbot
Copy link
Collaborator Author

From @GeoffreyBooth on 2017-05-05 14:21

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.

@coffeescriptbot
Copy link
Collaborator Author

From @joeldrapper on 2017-05-06

@GeoffreyBooth Why are they discouraged?

@coffeescriptbot
Copy link
Collaborator Author

From @GeoffreyBooth on 2017-05-06

See coffeescript6/discuss#17

@coffeescriptbot
Copy link
Collaborator Author

From @joeldrapper on 2017-05-06

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.

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

No branches or pull requests

1 participant