{"issue":{"milestone":null,"title":"Fat-arrow syntax for instance-bound method declaration is inconsistent","created_at":"2011-12-29T01:01:56Z","user":{"login":"jaekwon","id":53785,"url":"https://api.github.com/users/jaekwon","avatar_url":"https://secure.gravatar.com/avatar/44f47adfcff58848fd96e055c04437d0?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"44f47adfcff58848fd96e055c04437d0"},"updated_at":"2011-12-30T02:42:30Z","labels":[{"name":"question","url":"https://api.github.com/repos/jashkenas/coffee-script/labels/question","color":"cc317c"}],"state":"open","id":2676338,"closed_at":null,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"html_url":"https://github.com/jashkenas/coffee-script/issues/1984","body":"Currently, as we all know, this is how you declare instance-bound methods in CoffeeScript:\r\n\r\n```coffeescript\r\nclass Foo\r\n  method: => \"#{this} is an instance of Foo.\"\r\n```\r\n\r\nI think this is confusing because `this` normally refers to the class (`Foo`) in the class declaration scope.\r\nThat is, `: =>` is special, and it shows when doing metaprogramming (or decorators) with instance-bound methods.\r\n\r\n```coffeescript\r\nclass Foo\r\n  this::[\"dynamicMethod\"] = (args…) => … # this function is bound to Foo, not the instance.\r\n\r\nmyDecorator = (fn) -> fn # dummy decorator\r\n\r\nclass Bar\r\n  method: myDecorator => … # this function is bound to Bar, not the instance.\r\n```\r\n\r\nI'm interested in ways to resolve this inconsistency by separating instance-binding from the function declaration.\r\n\r\nOne way is with a new operator `:= (set-key-and-bind-to-object)` -- it shifts the instance-binding declaration to the left of the method declaration, which allows the coexistence of concise method decorators AND binding.\r\n\r\n```coffeescript\r\nclass Foo\r\n  method1: decorator (args...) => ...    # Problem: This is NOT bound to the instance\r\n  method2: => decorator (args...) => ... # Nonsolution: This is just ugly\r\n  method3:= (args...) -> ...             # Idea: New way of declaring bound methods\r\n  method4:= decorator (args...) -> ...   # Also works with decorators.\r\n\r\n# Maybe it also works on regular objects\r\nmyObject =\r\n  key1: \"value1\"\r\n  key2: \"value2\"\r\n  key3:= (args...) -> \"This is also bound to myObject\"\r\n```\r\n\r\n… but this still relies on compile-time analysis which does not lend itself to consistent behavior with other forms of metaprogramming. [Cardamom/base.coffee](https://github.com/jaekwon/Cardamom/blob/master/src/base.coffee) is another proof of concept that moves the instance-binding declaration into the runtime.\r\n\r\n```coffeescript\r\nclass Foo extends Base\r\n  @instance                                # Functions declared here will be bound to the instance\r\n    method1: (args...) -> ...              # A bound method\r\n    method2: decorator (args...) -> ...    # A bound method with a decorator. Nothing inconsistent.\r\n    method3: (args...) => ...              # A method bound to the class Foo. Not too useful.\r\n    method4: decorator (args...) => ...    # Maybe the decorator does something useful.\r\n  dynamicMethods.each (methodName) =>\r\n    @instance methodName, (args...) -> ... # Metaprogramming with binding.\r\n                                           # You couldn't do this with the := operator above\r\n                                           # This is runtime code.\r\n```\r\n\r\nAre there other ways besides the two proposed here, and is anybody else interested in any of them?","number":1984,"url":"https://api.github.com/repos/jashkenas/coffee-script/issues/1984","comments":2,"assignee":null},"comment":{"created_at":"2011-12-30T02:42:30Z","user":{"login":"jaekwon","id":53785,"url":"https://api.github.com/users/jaekwon","avatar_url":"https://secure.gravatar.com/avatar/44f47adfcff58848fd96e055c04437d0?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"44f47adfcff58848fd96e055c04437d0"},"updated_at":"2011-12-30T02:42:30Z","id":3309317,"body":"I like it too, now that I understand it. I'm pretty happy with the Base class approach for now.","url":"https://api.github.com/repos/jashkenas/coffee-script/issues/comments/3309317"},"action":"created"}