jashkenas/coffee-script — Issue #2136: Sugar for function binding, uncurrying `this` and setting `this` context
Author: shesek
Created: 2012-02-24 06:51:41+00:00
State: open

Somewhat based on a syntax [proposed to es-discuss](https://mail.mozilla.org/pipermail/es-discuss/2012-February/020699.html), I would like to suggest the following additions:

- Setting the `this` context for function applications:

```coffee
func@obj a, b, c # Desugars to `func.call obj, a, b, c`
func@obj a..., b # Desugars to `func.apply obj, [a..., b]`
```

- Binding functions to objects:

```coffee
func@obj # Desugars to [the ES3-equivalent of] `func.bind obj`
```

Possibly even `func@@` as sugar for `func@this`? Could be useful as a fat-arrow version for non-literal functions.

- Uncurrying `this` (the original propsal es-discuss was suggesting that)

```coffee
func@ # Desugars to [the ES3-equivalent of] `Function::call.bind func`
# Primary use-case is for passing-in a function that expects `this` rather than an argument, to somewhere that passes the data as an argument, like `map`:
['foo', 'bar', 'baz'].map String::trim@
```

For complex expressions on the RHS or LHS of the `@`, parentheses should be used, e.g. `func@(foo bar)`, `(baz qux)@obj`, `(baz qux)@(foo bar)`