satyr / coco

Unfancy CoffeeScript
http://satyr.github.com/coco/
MIT License
497 stars 48 forks source link

Destructuring catch parameter #183

Closed gkz closed 12 years ago

gkz commented 12 years ago

Something like:

try
  ...
catch {message: msg}
  console.log msg
satyr commented 12 years ago

Worth sacrificing implicit block after catch?

gkz commented 12 years ago

I think it's more consistent, and it's useful. I've implemented it: https://github.com/gkz/LiveScript/commit/4a3911c935afeccc591a8a64d336a065b72afbcb, https://github.com/gkz/LiveScript/commit/ddca6cb3fee4467d0076aebcef1138ac8b6a557e You can backport it if you decide it's worth it.

michaelficarra commented 12 years ago

CoffeeScriptRedux also allows destructuring there and anywhere else an assignment is happening, implicit or explicit.

satyr commented 12 years ago

I think it's more consistent,

I gave catch implicit block as I thought it's more consistent (along with try and finally).

and it's useful.

Been with this feature in SpiderMonkey for a quite while, I found it useful only when I was explicitly ignoring the error variable (i.e. catch([]){}), led me to advocate the catchless in CoffeeScript.

allows destructuring there and anywhere else an assignment is happening

We don't do this everywhere just because we can, for YAGNI and KISS. for's index is another example (as non-variable LeftHandSideExpression on JS for-in is extraordinarily rare).

michaelficarra commented 12 years ago

@satyr: The "simple" in KISS does not mean "easy", but instead promotes only requiring the user to have a simple understanding of assignment. Whenever something is being assigned, you can use destructuring. That is a simple rule to understand. For the same reason, this is allowed:

$ bin/coffee --js --cli 'a = {}; fn = (a.b) -> a.b'
// Generated by CoffeeScript 2.0.0-dev
void function () {
  var a, fn;
  a = {};
  fn = function (param$) {
    a.b = param$;
    return a.b;
  };
}.call(this);

And that is "simpler" than remembering what values are legal parameters.

satyr commented 12 years ago

That KISS was mainly for my lazyness (simple imple). Coco allows that one:

$ coco -bce '(a.b) ->'
(function(b){
  a.b = b;
});

primary because it was simpler to do than specifically allow @-vars.