Skip to content

Instantly share code, notes, and snippets.

@MadcapJake
Last active June 24, 2022 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MadcapJake/02662746cbca1404756cfced8db97973 to your computer and use it in GitHub Desktop.
Save MadcapJake/02662746cbca1404756cfced8db97973 to your computer and use it in GitHub Desktop.

If you're going to do the @@ sigil, why not just place it directly on the name? The getting and setting could be determined from function signature.

class Foo
  @@random: -> Math.random()
  @@name: -> Db.query 'name'
  @@name: (newName) -> Db.update 'name', newName

I don't particularly like how close it is to @ while being very different in action.

What about a named attribute like property? You could also go with a shorter prop but I think the full version fits more with CoffeeScript ethos.

class Foo
  property random: -> Math.random()
  property name: -> Db.query 'name'
  property name: (newName) -> Db.update 'name', newName

This might be easier to implement since it's basically a function with an object argument.

You could also go down a similar path to some other languages and add a symbol after the slot name:

class Foo
  random^: -> Math.random()
  name^: -> Db.query 'name'
  name=: (newName) -> Db.update 'name', newName

^ being completly original whereas = has precdent.

I also think hacking the computed property syntax looks ok but would still lead to reserved usage albeit inside of computed property expressions only.

class Foo
  [get random]: -> Math.random()
  [get name]: -> Db.query 'name'
  [set name]: (newName) -> Db.update 'name', newName

For that matter, I don't mind the {get foo}: idea mentioned above in a similar vein with less conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment